I am practicing python functions tonight and I come over a problem. I was trying to make a reservations program and it did not seem to work. Here's my code so far:
def make_res():
res = []
answer = 'y'
resnum = 0
while True:
resnum = resnum 1
print()
date = input('Date:')
time = input('Time:')
name = input('Name:')
people = input('No. of People:')
data = res.append({resnum,date,time,name,people})
view_res(data)
repeat = input("Repeat again? [y/n]\t")
if repeat == answer:
continue
if repeat != answer:
welcome_page()
def view_res(data):
reserve = data
print(reserve)
back = input("Do you want to go back to main menu? [y/n]\t")
if back == 'y':
welcome_page()
else:
exit()
def welcome_page():
print("System Menu")
print("\ta. View all Reservations")
print("\tb. Make reservations")
print()
selection = input("Enter selection: ")
if selection == "a" or selection == "A":
view_res()
elif selection == "b" or selection == "B":
make_res()
else:
print("Invalid input!")
welcome_page()
where did I go wrong? I am very new to this language, please help me
CodePudding user response:
As mentioned in the comments, there are quite a few issues w/ your code. Have you started with basic python tutorials? This is a good book to get you started with some examples on functions and data types:
https://greenteapress.com/thinkpython/html/index.html
It has many examples and small exercises to get you going.
For your program above, here are a few pointers:
When you make a function, think about what inputs (parameters) are needed and what the function should return. It should do one thing.
For the "main part" of your program, you appear to want to have a loop that does one of two things based on input. You do NOT need to put the "main loop" in a function, just write it and have it call one of the functions, and have a mechanism to retain the reservations.
Here is a partial framework that you can work with and some comments to help you along:
# reservation maker
def make_reservation(): # no paramters are needed
# ask some questions to get people, place, etc.
# make the reservation data object, probably a list to start with...
new_reservation = [people, place, etc.]
return new_reservation
def list_all_reservations(reservations):
# make a loop to loop through the reservations, which is probably
# a list-of-lists to start with
for r in reservations:
print(r)
# no return is needed for a "print" function, it will return "None" automatically
# make your main loop and have a container to hold the reservations. This should NOT
# be in a function
# set up the main loop...
all_reservations = [] # to hold all of the reservations...
while True:
# ask your questions about what to do, call the functions as needed...
# something like...
# ask the question, catch the result in a variable called "response"...
# set up your if-else based on response...
if response.lower() == 'a':
new_res = make_reservation()
all_reservations.append(new_res)
elif response.lower() == 'b':
list_all_reservations(all_reservations)
else:
break # stop the loop