I'm trying to create a loop if the condition is not met. If it is met at minimum second run, the print from my last run is also in my output, like in the example its printing the same print() that was printed in the previous run in addition to the result of the second run.
secretcode= r.randint(1, 100)
print("\n\n\nWelcome to the lottery!\n")
print("Guess a number from 1 to 100!")
print(secretcode)
def usrinput():
usrinputnumber = int(input("Your number: "))
schleife(usrinputnumber)
def schleife(usrinputnumber):
while not (secretcode == usrinputnumber):
if usrinputnumber > secretcode:
print("Awh damn! Your number is too high!!")
usrinput()
else:
print("Unlucky! YOur number is too low!")
usrinput()
print("Congratz! You guessed the number!!")
usrinput()
Everything works fine when I do an "if" instead the while and using the last print as "else"
Example: The "secretcode" is 55. As input I type 45. The console prints "Unlucky! The number is too low!" Now I type 55. The console prints the "Congratz" message AND the "too low" message. Why the "too low" message?
I know functions are not needed, I just wanna know what the problem is and if I can run it in functions like I did.
Thank you very much!