Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
userChoice2 = input("What is the coin type you want to count?: ")
while userChoice2 not in ["1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2"]:
       print("Wrong input")
       print("You have to pick one from" "1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2")



If an invalid response is entered the program continuously prints the final two print commands.

What I have tried:

I have tried using a function and making the while loop true and false.
Posted
Updated 11-Jan-20 4:23am
Comments
[no name] 11-Jan-20 5:47am    
input needs to be inside the while loop
Coding1014 11-Jan-20 5:50am    
Where in the while loop?

Look at your code: the loop consists of three lines:
while userChoice2 not in ["1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2"]:
       print("Wrong input")
       print("You have to pick one from" "1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2")
But none of them change the value in userchoice2 so once you enter the loop with a bad input, you will continually check the same bad value and print the same messages.
What you need to do is copy the input line so that after you print the error messages, you can get a new value:
userChoice2 = input("What is the coin type you want to count?: ")
while userChoice2 not in ["1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2"]:
       print("Wrong input")
       print("You have to pick one from" "1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2")
       userChoice2 = input("What is the coin type you want to count?: ")
 
Share this answer
 
v2
Try also
Python
allowed =  ["1p", "2p", "5p", "10p", "20p", "50p", "£1", "£2"]
while True:
  userChoice2 = input("What is the coin type you want to count?: ")
  if userChoice2 in allowed:
    break
  print("you have to pick one from ", allowed)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900