patternpythonMinor
Python Quiz CLI & GUI
Viewed 0 times
cliquizguipython
Problem
I recently got feedback for a programming assignment that was positive but mentioned that 'some things could be done better', and I was wondering if anybody here might be able to give me a more specific idea of what could to be improved & why.
There's quite a bit of code, but as I'm at a very introductory level I'm hoping it won't be too difficult to pick out some basic concepts that need work. (Also, I'm not too worried about how the functionality of the program could be improved - that was informed by the guidelines of the assignment - I'm more looking for advice on how to improve the code for the functionality that is currently there).
The assignment was to create a food quiz GUI that reads data from a txt file, & a CLI admin that creates/edits that file.
CLI:
```
import json
# Repeatedly prompt for input until an integer is entered.
def inputInt(prompt):
while True:
try:
answer = int(input(prompt))
except ValueError:
print('Please enter a number.')
continue
break
return answer
# Repeatedly prompt for input until something (not whitespace) is entered.
def inputSomething(prompt):
answer = input(prompt)
while not answer.strip():
answer = input('Please enter a non-whitespace value: ')
return answer
# Open "data.txt" in write mode and write the data to it in JSON format.
def saveChanges(dataList):
f = open('data.txt', 'w')
json.dump(dataList, f, indent=4)
f.close()
# If the file does not exist or does not contain JSON data, set "data" to an empty list instead.
try:
f = open('data.txt', 'r')
data = json.load(f)
f.close()
except (FileNotFoundError, ValueError):
data = []
# Print welcome message, then enter the endless loop which prompts the user for a choice.
print('Welcome to the Food Quiz Admin Program.')
while True:
print('Choose [a]dd, [l]ist, [s]earch, [v]iew, [d]elete or [q]uit.')
choice = input('> ')
if choice == 'a
There's quite a bit of code, but as I'm at a very introductory level I'm hoping it won't be too difficult to pick out some basic concepts that need work. (Also, I'm not too worried about how the functionality of the program could be improved - that was informed by the guidelines of the assignment - I'm more looking for advice on how to improve the code for the functionality that is currently there).
The assignment was to create a food quiz GUI that reads data from a txt file, & a CLI admin that creates/edits that file.
CLI:
```
import json
# Repeatedly prompt for input until an integer is entered.
def inputInt(prompt):
while True:
try:
answer = int(input(prompt))
except ValueError:
print('Please enter a number.')
continue
break
return answer
# Repeatedly prompt for input until something (not whitespace) is entered.
def inputSomething(prompt):
answer = input(prompt)
while not answer.strip():
answer = input('Please enter a non-whitespace value: ')
return answer
# Open "data.txt" in write mode and write the data to it in JSON format.
def saveChanges(dataList):
f = open('data.txt', 'w')
json.dump(dataList, f, indent=4)
f.close()
# If the file does not exist or does not contain JSON data, set "data" to an empty list instead.
try:
f = open('data.txt', 'r')
data = json.load(f)
f.close()
except (FileNotFoundError, ValueError):
data = []
# Print welcome message, then enter the endless loop which prompts the user for a choice.
print('Welcome to the Food Quiz Admin Program.')
while True:
print('Choose [a]dd, [l]ist, [s]earch, [v]iew, [d]elete or [q]uit.')
choice = input('> ')
if choice == 'a
Solution
Very nice! You're off to a strong start.
In your CLI code, I'd put the code for each option in its own function, rather than having all of that in the main block. It makes it a lot easier to follow, and wouldn't require much work.
Also, FYI, 'protien' is spelled 'protein'.
In your CLI code, I'd put the code for each option in its own function, rather than having all of that in the main block. It makes it a lot easier to follow, and wouldn't require much work.
Also, FYI, 'protien' is spelled 'protein'.
Context
StackExchange Code Review Q#149072, answer score: 2
Revisions (0)
No revisions yet.