Answer the question
In order to leave comments, you need to log in
How to correctly adjust the file output and their values?
There is a name that is included in the file, such as Quor. When the program starts, it checks for this name, it finds it, and it prompts you to enter a different username to be added to the file. But when I enter a new name, in the output place it displays the name Quor, instead of the new one entered, it drives in the same Quor, or the first one entered.
I can not understand.
import json
def get_stored_username():
"""Получает хранимое имя пользователя, если оно существует."""
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
"""Запрашивает новое имя пользователя."""
username = input("What is your name? ")
filename = 'username.json'
with open(filename, 'a') as f_obj:
json.dump(username, f_obj)
return username
def great_user():
"""Приветствует пользователя по имени."""
username = get_stored_username()
if username:
print("Welcome back, " + username + "!")
else:
username = get_new_username()
print("We'll remember you when you come back, " + username + "!")
def search_user():
username = get_new_username()
if username == "Dort":
print("Это имя уже есть, введите новое значение: ")
get_new_username()
print("We'll remember you when you come back, " + username + "!")
else:
great_user()
search_user()
Answer the question
In order to leave comments, you need to log in
1. It is strange to store only one line in json format - a name, without keys, without anything.
2. It is not clear why to add the file if you can simply dump the old information into it and add the new one
3. get_new_username()
No username was assigned here, so
print("We'll remember you when you come back, " + username + "!")
shows the old name.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question