I need to create a code using loops to ask for user input on certain values. Of those values I need to ask the user if they want me to find the smallest or largest number or just end the program. If the number -1000 is entered in the program the program ends. Basically how do I link my menu of options to a the actual actions per option.Here's what I have so far.
numbersEntered = []
lengthNumbers = int(input("Please enter the length of your list/array:"))
print("Please enter your numbers individually:")
for x in range(lengthNumbers):
data=int(input())
numbersEntered.append(data)
def menu():
print("[A] Smallest")
print("[B] Largest")
print("[C] Quit")
menu()
Options=float(input(f"Please select either option A,B,or C:"))
optionA = min(numbersEntered)
optionB = max(numbersEntered)
optionC = quit
while numbersEntered != C:
if numbersEntered == A:
print("The smallest number is:", min(numbersEntered))
elif numbersEntered == B:
print("The largest number is:", max(numbersEntered) )
elif numbersEntered ==-1000:
print("Quit")
I tried a while loop to connect the menu to said action but that did not work and idk why. I am a beginner programer so I'm very new to this stuff.
CodePudding user response:
Your while loop is incorrect. The correct way is to receive user input as a string and take action accordingly. The following is a working sample:
numbersEntered = []
lengthNumbers = int(input("Please enter the length of your list/array: "))
print("Please enter your numbers individually: ")
for x in range(lengthNumbers):
data = int(input())
if data == -1000:
print("-1000 received, exiting")
exit()
numbersEntered.append(data)
def menu():
print("[A] Smallest")
print("[B] Largest")
print("[C] Quit")
menu()
while True:
option = input("Please select either option A,B,or C: ")
if option == "A":
print("The smallest number is:", min(numbersEntered))
elif option == "B":
print("The largest number is:", max(numbersEntered))
elif option == "C":
print("Quit")
break
CodePudding user response:
You are close, but...
The program doesn't really exit and bypass the remainder of the program if -1000 is entered.
The input function is being converted to a float type instead of a string 'A', 'B', or 'C'.
The while loop and underlying if statements have issues with the type and value being compared as well as not exiting if 'C' was entered.
Here is a pass at the fixed code... There are a lot of ways to solve this. You were close so I added extra comments that I hope helps you with the flow and solving your next programming puzzle.
numbersEntered = []
# moved functions outside program flow
def menu():
print("[A] Smallest")
print("[B] Largest")
print("[C] Quit")
#############################################
# program logic starts here.
# You could put all this in a main() function
#############################################
lengthNumbers = int(input("Please enter the length of your list/array:"))
print("Please enter your numbers individually:")
for x in range(lengthNumbers):
data=int(input())
numbersEntered.append(data)
# added a check for -1000 to exit the input loop
if (data == -1000):
break
# removed the OptionA, B, C and values as they aren't necessary
# changed the while condition to skip if the user chose to exit in the
# input section
while numbersEntered[-1] != -1000:
# moved the menu and input inside the while loop to repeat the menu
menu()
# changed input from float to str and convert to uppercase
Options=str(input(f"Please select either option A,B,or C:")).upper()
# compare to strings ('A', 'B', or 'C') vs value of A, B, or C
if Options == 'A':
print("The smallest number is:", min(numbersEntered))
elif Options == 'B':
print("The largest number is:", max(numbersEntered) )
# check for 'C' input from user to break out of the while loop
elif Options == 'C':
break
# move the exit/quit message to the end of the program
print('Exited Program')