Python 调用函数进行重新匹配? def restart(): 随机输入 导入时间 opp=0 计数=0 右=0 时间。睡眠(1.5) 打印(“E表示简单,M表示中等,H表示硬!”) 时间。睡眠(1.5) 选择=输入(“您选择什么?:”) 打印(“”) 如果选项==“E”: 打印(“您选择了简单难度!”) elif choice==“M”: 打印(“您选择了中等难度!”) elif choice==“H”: 打印(“您选择了困难难度!”) 其他: 打印(“无效输入”) 打印(“”) 重新启动() 时间。睡眠(2) 打印(“”) 打印(“准备好了吗?”) 打印(“”) 时间。睡眠(2) 打印(“放手!”) 打印(“”) 时间。睡眠(2) def重新匹配(): 如果选项==“E”: while(count
为什么要使用如此多的调用来睡眠呢?Python 调用函数进行重新匹配? def restart(): 随机输入 导入时间 opp=0 计数=0 右=0 时间。睡眠(1.5) 打印(“E表示简单,M表示中等,H表示硬!”) 时间。睡眠(1.5) 选择=输入(“您选择什么?:”) 打印(“”) 如果选项==“E”: 打印(“您选择了简单难度!”) elif choice==“M”: 打印(“您选择了中等难度!”) elif choice==“H”: 打印(“您选择了困难难度!”) 其他: 打印(“无效输入”) 打印(“”) 重新启动() 时间。睡眠(2) 打印(“”) 打印(“准备好了吗?”) 打印(“”) 时间。睡眠(2) 打印(“放手!”) 打印(“”) 时间。睡眠(2) def重新匹配(): 如果选项==“E”: while(count,python,python-3.x,Python,Python 3.x,为什么要使用如此多的调用来睡眠呢?这是不必要的 我已经为您清理了一些东西,并为您提供了一个可用于构建的最小示例。 我还没有实现难度等级;一旦你理解了程序的基本知识,你可以尝试实现它;我的方法是将难度等级与随机生成的数字范围联系起来 玩得开心 def restart(): import random import time opp = 0 count = 0 right = 0 time.sleep (1.5) print ("E fo
这是不必要的
我已经为您清理了一些东西,并为您提供了一个可用于构建的最小示例。
我还没有实现难度等级;一旦你理解了程序的基本知识,你可以尝试实现它;我的方法是将难度等级与随机生成的数字范围联系起来
玩得开心
def restart():
import random
import time
opp = 0
count = 0
right = 0
time.sleep (1.5)
print ("E for easy, M for medium, H for hard!")
time.sleep (1.5)
choice = input("What do you choose?: ")
print ("")
if choice == 'E':
print("You have chosen easy difficulty!")
elif choice == 'M':
print("You have chosen medium difficulty!")
elif choice == 'H':
print("You have chosen hard difficulty!")
else:
print("Invalid input")
print ("")
restart()
time.sleep (2)
print ("")
print ("Are you ready?")
print ("")
time.sleep (2)
print ("Lets go!")
print ("")
time.sleep (2)
def rematch():
if choice == "E":
while (count <= 9):
num1 = random.randint(1,5)
num2 = random.randint(1,5)
opp = random.randint(1,2)
if opp == 1:
opp = ("+")
elif opp == 2:
opp = ("-")
print("what is " + str(num1) + str(opp) + str(num2)+ "")
answer = (input("It is... "))
if opp == "+":
qanswer = str(num1+num2)
elif opp == "-":
qanswer = str(num1-num2)
if answer == qanswer:
right = right + 1
print ("You got it right!")
time.sleep (1)
print ("")
else:
print ("You got it wrong...")
time.sleep (0.5)
if opp == "+":
print ("The correct answer was " + str(num1+num2) + "")
print ("")
elif opp == "-":
print ("The correct answer was " + str(num1-num2) + "")
print ("")
time.sleep (1)
count = count + 1
print ("you got " + str(right) + " out of 10!")
yeet = (input("Would you like to rematch, restart, or quit?: "))
if yeet == ("restart"):
print ("")
restart()
elif yeet == ("rematch"):
print ("")
rematch()
elif yeet == ("quit"):
print ("See you next time!")
print ("")
time.sleep (2)
exit()
else:
print ("Invalid input,")
time.sleep (1)
print ("Please input either rematch, restart or quit")
随机导入
导入时间
def start():
全球opp,计数,对,选择
opp=0
计数=0
右=0
尽管如此:
选择=输入(“您选择的难度是多少?\n”)
尝试:
打印(f“您选择了{defestion[choice]}defestion!”)
打破
除KeyError外:
打印(“请输入‘E’、‘M’或‘H’:”)
打印(“准备好了吗?”)
时间。睡眠(2)
打印(“放手!”)
def play():
全球opp,计数,对,选择
#如果选择==“E”:#以后再实施
当计数小于10时:
num1=random.randint(1,5)
num2=random.randint(1,5)
opp=random.choice(“+”、“-”)
打印(f“什么是{num1}{opp}{num2}”)
回答=输入(“它是…”)
qanswer=str(num1+num2)如果opp==“+”否则str(num1-num2)
如果回答==qanswer:
右=右+1
打印(“你做对了!”)
其他:
打印(“你搞错了…”)
打印(f“正确答案是:{qanswer}”)
计数=计数+1
打印(f“你得到了10分中的{正确}!”)
yeet=input(“您想重新匹配(Y,N)?:”)
如果yeet==“Y”:
开始()
其他:
回来
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
难度={'E':'easy','M':'medium','H':'hard'}
opp=0
计数=0
右=0
选择=无
开始()
play()
你的问题是什么?请参阅指导原则——问题中的一个好的代码示例将是最短的事情,可以让其他人重现一个清晰的问题,并清楚地区分实际的和期望的输出。至少90%的代码看起来与问题没有任何关系……也就是说,我想知道这是否是一个重复在restart
中,您从未调用过rematch
。为什么希望它运行?至少,在定义rematch()
之后,您需要从restart()
中的某个位置调用rematch()
。但是将rematch()
升级到与restart()相同的级别可能是个好主意
并根据用户的选择从restart()调用它。
除此之外,从函数本身调用函数并不是一个好主意——最终可能会有太多副本运行,导致Python崩溃(例如,如果重新启动1000次)。最好设置一个外部循环,继续调用restart()
或重新匹配()
直到程序完成。
import random
import time
def start():
global opp, count, right, choice
opp = 0
count = 0
right = 0
while True:
choice = input("What difficulty do you choose? \n")
try:
print(f"You have chosen {difficulty[choice]} difficulty!")
break
except KeyError:
print("please enter 'E', 'M', or 'H': ")
print("Are you ready?")
time.sleep (2)
print("Lets go!")
def play():
global opp, count, right, choice
# if choice == "E": # implement that later
while count < 10:
num1 = random.randint(1, 5)
num2 = random.randint(1, 5)
opp = random.choice(('+', '-'))
print(f"what is {num1} {opp} {num2}")
answer = input("It is... ")
qanswer = str(num1 + num2) if opp == "+" else str(num1 - num2)
if answer == qanswer:
right = right + 1
print("You got it right!")
else:
print("You got it wrong...")
print(f"The correct answer was: {qanswer}")
count = count + 1
print(f"you got {right} out of 10!")
yeet = input("Would you like to rematch (Y, N)?: ")
if yeet == "Y":
start()
else:
return
if __name__ == '__main__':
difficulty = {'E': 'easy', 'M': 'medium', 'H': 'hard'}
opp = 0
count = 0
right = 0
choice = None
start()
play()