This question has been solved.
CodePudding user response:
enter image description hereUse loop while like this instead of loop for
CodePudding user response:
Just change:
if event != "Dancing" or "Singing" or "Eating":
To:
if event not in ["Dancing", "Singing", "Eating"]:
This will correctly compare event
to all of the options.
Currently, your code compares event
to "Dancing"
and then evaluates if "Singing"
and "Eating"
are Truthy (and they are because they are not empty. This means that the if
will evaluate True
every time.
Example output:
How many events would you like to choose from? 1
Choose an event: Dancing
You chose ['Dancing']
Have fun!
Full code here:
user_events = []
i = 0
print("Welcome! You can choose any of the following events to participate in \n1. Dancing \n2. Singing \n3. Eating \n")
num_events = int(input("How many events would you like to choose from? "))
for i in range (0, num_events):
event = input ("Choose an event: ")
#for j in resort_events:
if event not in ["Dancing", "Singing", "Eating"]:
print("That's not one of our special events!")
elif event == "Dancing":
user_events.append(event)
elif event == "Singing":
user_events.append(event)
elif event == "Eating":
user_events.append(event)
print(f"You chose {user_events}")
print("Have fun!")
CodePudding user response:
First, you don't need to declare the variable "i" since you're again declaring it in for loop.
Then, your if statements are not in for loop that's why events are not being appended. I have corrected your code:
user_events = []
print("Welcome! You can choose any of the following events to participate in \n1. Dancing \n2. Singing \n3. Eating \n")
num_events = int(input("How many events would you like to choose from? "))
for i in range(num_events):
event = input("Choose an event: ")
if event == "Dancing":
user_events.append(event)
elif event == "Singing":
user_events.append(event)
elif event == "Eating":
user_events.append(event)
else:
print("That's not one of our special events!")
print(f"You chose {user_events}")
print("Have fun!")
I have also removed your if statement because the user has only three choices i.e Dancing, Singing, and Eating other than that is surely not needed so it's better to put those choices in if/elif conditions that are actually needed, remaining will go to the else.
Output:
Welcome! You can choose any of the following events to participate in
1. Dancing
2. Singing
3. Eating
How many events would you like to choose from? 2
Choose an event: Singing
Choose an event: Eating
You chose ['Singing', 'Eating']
Have fun!
CodePudding user response:
user_events = []
i = 0
print("Welcome! You can choose any of the following events to participate in \n1. Dancing \n2. Singing \n3. Eating \n")
num_events = int(input("How many events would you like to choose from? "))
for i in range (0, num_events):
event = input ("Choose an event: ")
#for j in resort_events:
if event != "Dancing" and event != "Singing" and event != "Eating":
print("That's not one of our special events!")
elif event == "Dancing":
user_events =["Dancing"]
elif event == "Singing":
user_events =[event]
elif event == "Eating":
user_events =[event]
print(f"You chose {user_events}")
print("Have fun!")
Your If statement checking whether or not the user input matches the options is incorrect. as your conditions are 1. event != Dancing 2. "Singing" 3. "Eating". You need to repeat the "event != x" part. Because "Singing" as a condition would return False which would make the if statement return false each time.
CodePudding user response:
Try this. It loops through the list to output it and there was a bug with not having the if statement in the for loop which cause only the last input to be registered. Also you dont need that many if statements as if it is any of the events, there will be the same outcome.
user_events = []
i = 0
print("Welcome! You can choose any of the following events to participate in \n1. Dancing \n2. Singing \n3. Eating \n")
num_events = int(input("How many events would you like to choose from? "))
for i in range (0, num_events):
event = input ("Choose an event: ")
if event != "Dancing" and event != "Singing" and event != "Eating":
print("That's not one of our special events!")
else:
user_events.append(event)
print("You chose " ,end = "")
for i in user_events:
print(i, end = "")
print("")
print("Have fun!")