[英]Why does my function keep looping and how can i fix it?
import time
import random
import sys
def code():
user_num=()
user_num=int(input("What number do you want from 0-30"))
if user_num>30:
print("number needs to be smaller")
print("restart code and try again")
else:
pass
if user_num<0:
print("your number needs to be greater")
print("restart code and try again")
else:
pass
code()
code()
random_num=random.randint(0,1)
if random_num==user_num:
print("your number is correct")
else:
print("your number is incorrect")
time.sleep(1)
try_again=input("do you want to try again (yes/no")
if try_again=="yes":
code()
else:
print("ok. Bye")
i am very new to functions so sorry if this is a rookie mistake. 我对功能非常陌生,如果这是菜鸟的错误,抱歉。 Any help with functions will be appreciated.
任何有关功能的帮助将不胜感激。 Thank You.
谢谢。
在“代码”功能的最后,您要再次调用它
Try this: 尝试这个:
import time
import random
import sys
def code():
user_num=()
user_num=int(input("What number do you want from 0-30"))
if user_num>30:
print("number needs to be smaller")
print("restart code and try again")
else:
pass
if user_num<0:
print("your number needs to be greater")
print("restart code and try again")
else:
pass
return user_num
random_num=random.randint(0,1)
user_num = code()
if random_num==user_num:
print("your number is correct")
else:
print("your number is incorrect")
time.sleep(1)
try_again=input("do you want to try again (yes/no")
if try_again in "yes":
user_num = code()
else:
print("ok. Bye")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.