0

How do i exit nested conditionals inside a loop?

Program keeps looping through the secondary conditional https://code.sololearn.com/cpZ98ylz0JH4/?ref=app

30th Mar 2019, 4:29 PM
Jesse Douglas
Jesse Douglas - avatar
7 Answers
+ 2
# Program intro message here def winner(answer): print('Congratulations!!!') print('You guessed the correct number!') print(answer) while (True): userGuess = int(input('Your guess:')) print(userGuess) randNum = random.choice(numbers) if userGuess == randNum: winner(randNum) else: print('Sorry your guess was incorrect') if userGuess > randNum: print('Your guess was too high') elif userGuess < randNum: print('Your guess was to low') print(f"Correct answer was {randNum}") tryAgain = input('Try Again? Y or N: ') print(f"{tryAgain}\n") if tryAgain.upper() == 'Y': continue elif tryAgain.upper() == 'N': break print('End of Program') Please confirm if anything was unclear, I'll try best to explain. Hth, cmiiw
30th Mar 2019, 5:25 PM
Ipang
+ 1
No, it doesn't always have to be, I considered this loop to repeat while True simply because there are different route to take for when user guess was correct or not. We could either start over or exit the loop depending on the correctness of the answer, and, <tryAgain> in case of wrong answer.
30th Mar 2019, 6:12 PM
Ipang
+ 1
.upper() turns each alphabet character in a string to its uppercase form. "abc" -> "ABC" .lower() turns each alphabet character in a string to its lowercase form. "ABC" -> "abc" I guess it will ignore non alphabet characters on its work.
30th Mar 2019, 9:11 PM
Ipang
+ 1
You can use try...catch block to handle exception. If you haven't get to the chapter already, you will learn about it in "Exception & Files" chapter. num = 0 okay = False while not okay: try: num = int(input("Enter an integer")) okay = True except ValueError: print("\nBad input, not a integer") print(f"\nGood! input was {num}")
30th Mar 2019, 9:41 PM
Ipang
0
Do While Loops always have to be in this specific syntax to parse correctly while (True): code goes here Or while(False): code goes here
30th Mar 2019, 6:04 PM
Jesse Douglas
Jesse Douglas - avatar
0
What is the difference between .upper() and .lower() ?
30th Mar 2019, 8:57 PM
Jesse Douglas
Jesse Douglas - avatar
0
How do I catch strings and tell the user that the input has to be an integer?
30th Mar 2019, 9:20 PM
Jesse Douglas
Jesse Douglas - avatar