gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
while True: newvalue = input("enter new data: (0 to exit) ") if newvalue == 0: break…
- User
while True: newvalue = input("enter new data: (0 to exit) ")
codeif newvalue == 0: break else: with open('test.json', 'r') as file: loaded = json.load(file) loaded.append(newvalue) with open('test.json', 'w') as file: json.dump(loaded, file) print("Saved to file")Why doesnt it exit when i press 0?
- Assistant
The issue is that the input function returns a string, not an integer. Therefore, the condition
if newvalue == 0:will never be true because you are comparing a string to an integer. To fix this issue, you can change the condition toif newvalue == '0':to compare the input string to the string '0'. Here is the corrected code:pythonwhile True: newvalue = input("enter new data: (0 to exit) ") if newvalue == '0': break else: with open('test.json', 'r') as file: loaded = json.load(file) loaded.append(newvalue) with open('test.json', 'w') as file: json.dump(loaded, file) print("Saved to file")With this change, the program should exit when you input '0'.