如何在python代码中向后发送提示?

2024-09-27 22:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我用randomint函数编写了一个简单的数字猜测游戏。你知道吗

假设是我写的,看起来有点像这样:

 import blah
 import blah

 print ("Hello, and welcome to the guesser")

 Answer1 = (randomint(1,5))
 Guess1 = int(input("Input a number 1-5")

 if Guess1 == Answer1:
      print ("You got lucky that time")

 elif Guess1 < 6 and != Answer1:
      #send back to guess again

 elif Guess1 > 5:
      print ("Invalid Number")
      #send back to guess again

这可能是一个非常愚蠢的问题和初学者的东西,但是,有没有一个功能,可以满足我的需要和希望,送人回来猜一次数字?你知道吗


Tags: andto函数importsendback数字blah
1条回答
网友
1楼 · 发布于 2024-09-27 22:23:10

您正在搜索的内容称为编程中的循环。你知道吗

你可以这样做:

import random

Answer1 = random.randint(1,5)

while True:
    Guess1 = int(input("Input a number 1-5 >>"))

    if Guess1 == Answer1:
        print ("You got luck this time")
        break

    elif Guess1 < 6 and Guess1 != Answer1:
        #send back to guess again
        print ("Try again!")
        continue

    elif Guess1 > 5:
        print ("Invalid Number")
        #send back to guess again
        continue

相关问题 更多 >

    热门问题