Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lovemycodesnippets/c1ee62746501dd0e27a442fde87b5387 to your computer and use it in GitHub Desktop.

Select an option

Save lovemycodesnippets/c1ee62746501dd0e27a442fde87b5387 to your computer and use it in GitHub Desktop.
def get_user_input():
    """Get user input for name, age, email, and phone number"""
    print("Please enter your information:")
    name = input("Name: ").strip()
    while not name:
        print("Name cannot be empty. Please try again.")
        name = input("Name: ").strip()
    age = input("Age: ").strip()
    while not age.isdigit() or int(age) <= 0:
        print("Please enter a valid age (positive number).")
        age = input("Age: ").strip()
    age = int(age)
    email = input("Email address: ").strip()
    while not email or '@' not in email:
        print("Please enter a valid email address.")
        email = input("Email address: ").strip()
    phone = input("Phone number: ").strip()
    while not phone:
        print("Phone number cannot be empty. Please try again.")
        phone = input("Phone number: ").strip()
    return name, age, email, phone
def save_to_file(name, age, email, phone):
    """Save user information to a file"""
    try:
        with open("user_info.txt", "a") as file:
            file.write(f"Name: {name}\n")
            file.write(f"Age: {age}\n")
            file.write(f"Email: {email}\n")
            file.write(f"Phone: {phone}\n")
            file.write("-" * 30 + "\n")  # Separator between entries
        print("Information saved successfully!")
    except Exception as e:
        print(f"Error saving to file: {e}")
def main():
    """Main function to run the program"""
    print("User Information Collector")
    print("=" * 30)
    # Get user input
    name, age, email, phone = get_user_input()
    # Display entered information
    print("\nEntered Information:")
    print(f"Name: {name}")
    print(f"Age: {age}")
    print(f"Email: {email}")
    print(f"Phone: {phone}")
    # Confirm save
    confirm = input("\nDo you want to save this information? (y/n): ").strip().lower()
    if confirm in ['y', 'yes']:
        save_to_file(name, age, email, phone)
        print("Thank you for providing your information!")
    else:
        print("Information not saved.")
if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment