0
How do i exit nested conditionals inside a loop?
Program keeps looping through the secondary conditional https://code.sololearn.com/cpZ98ylz0JH4/?ref=app
7 ответов
+ 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
+ 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.
+ 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.
+ 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}")
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
0
What is the difference between
.upper()
and
.lower()
?
0
How do I catch strings and tell the user that the input has to be an integer?