'Users is asked twice same question because of duplicated code
I am trying to create a kind of TV show social media programme to practice and improve my basic coding skills, but I am a bit stuck.
The programme has a menu where shows users what they can do: create a profile, modify the profile, review the profile, updating the profile with a new TV show and leaving the programme.
I created several functions but I reached a point where I cannot move forward. The functions I created are asking the user the same question twice. I do want to avoid that but I am not able to find any solution.
This is what I have got so far:
import os
def ask_option():
# Print menu and return the chosen option
print("\nMENU\n")
print("Click 1 to create a user profile.")
print("Click 2 to modify your user profile.")
print("Click 3 to review your user profile.")
print("Click 4 to update your user profile.")
print("Click 5 to leave the programme.")
opcion = int(input("\nWhat do you chose?\n"))
return opcion
def ask_data():
# Ask data to user and return
name = input("What's your name? ").capitalize()
surname = input("What's your surname?: ").capitalize()
year = int(input("When were you born? "))
age = 2022 - year
age = str(age)
favourite_show = input("What's your favourite TV show of all times? ").title()
show = input("What are you binge watching right now? ").title()
return name, surname, year, age, favourite_show, show
def save_data(name, surname, year, age, favourite_show, show):
# Save user's data in a file
file = open(name + ".user", "w", encoding = "UTF-8")
file.write(name+"\n"+surname+"\n"+age+"\n"+favourite_show+"\n"+show+"\n")
file.close()
def print_data(name, surname, year, age, favourite_show, show):
# Print user's data
profile = name, surname, year, age, favourite_show, show
print("Name:", name, surname)
print("Age:", age, "years old")
print("Favourite TV Show:", favourite_show)
print("Currently binge watching:", show)
return profile
def create_profile():
# Function that calls ask_data(), prints user profile created and calls print_data()
# Before creating a profile, should check if there is another profile with same user's name. If the user already exists, return a message. If user is new, create profile.
# moveforward = True
# while moveforward:
# name = input("What's your name? ").capitalize()
# if not os.path.isfile(name + ".user"):
# name, surname, year, age, favourite_show, show = ask_data()
# save_data(name, surname, year, age, favourite_show, show)
# print("\nYour profile has been created.\n")
# print_data(name, surname, year, age, favourite_show, show)
# moveforward = False
# else:
# print("\nThere is already a profile with that user name.")
name = input("What's your name? ").capitalize()
if os.path.isfile(name + ".user"):
print("There is already a profile with that user name.")
return
name, surname, year, age, favourite_show, show = ask_data()
save_data(name, surname, year, age, favourite_show, show)
print("\nYour profile has been created.\n")
print_data(name, surname, year, age, favourite_show, show)
def modificar_perfil():
(etc, etc)
So the thing here is that user's gets asked twice: What's your name inside the create_profile() functions and I do not know how to fix it.
Can anyone share any ideas? Much appreciated.
Solution 1:[1]
You are asking for name twice. Once in create profile, when you are checking to see if a profile has been created, and again in the first line of ask_data.
Perhaps move your profile checking logic into ask data.
def create_profile():
# ------ move this logic into ask_data, otherwise, you will ask again when it is called
name = input("What's your name? ").capitalize()
if os.path.isfile(name + ".user"):
print("There is already a profile with that user name.")
return
# ---------------------------
name, surname, year, age, favourite_show, show = ask_data()
save_data(name, surname, year, age, favourite_show, show)
print("\nYour profile has been created.\n")
print_data(name, surname, year, age, favourite_show, show)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amit Kulkarni |
