Python 我需要我的脚本运行几行,直到任务完成

Python 我需要我的脚本运行几行,直到任务完成,python,python-3.x,Python,Python 3.x,我需要在玩家猜到数字之前出现代码的询问部分 我是一个初学者,我正在写一个简单的代码来猜一个数字。我已经得到了我所需要的一切,但我需要它,这样脚本才能运行,直到他们猜到数字。我不确定我是忘了还是我的书已经过时了。谢谢你的帮助 randomnumber = random.randint(1,101) questioning = input("Guess a number!: ") answered_edited = int(questioning) randmnew = int(randomnum

我需要在玩家猜到数字之前出现代码的询问部分

我是一个初学者,我正在写一个简单的代码来猜一个数字。我已经得到了我所需要的一切,但我需要它,这样脚本才能运行,直到他们猜到数字。我不确定我是忘了还是我的书已经过时了。谢谢你的帮助

randomnumber = random.randint(1,101)

questioning = input("Guess a number!: ")
answered_edited = int(questioning)
randmnew = int(randomnumber)
if answered_edited == randmnew:
print("You've won! Just don't get too ahead of yourself /n since this was 
only a simple game any child could play.")
elif answered_edited < randmnew:
print("It's greater than that you twat")
elif answered_edited > randmnew:
print("You're a little low shawty")
randomnumber=random.randint(1101)
提问=输入(“猜一个数字:”)
已回答\u编辑=int(提问)
随机数=int(随机数)
如果已回答_edited==randmnew:
打印(“你赢了!只是不要太超前了/n因为这是
任何孩子都只能玩一个简单的游戏。”)
elif回答_编辑randmnew:
打印(“你有点低了,肖蒂”)
我想让脚本运行,并不断地问什么数字,直到玩家猜到

won=False
 won=False
 randomnumber = random.randint(1,101)
 while won==False:
         questioning = input("Guess a number!: ")
         answered_edited = int(questioning)
         randmnew = int(randomnumber)
         if answered_edited == randmnew:
                   print("You've won! Just don't get too 
                   ahead of yourself /n since this was 
                   only a simple game any child could play.")
                   won=True
         elif answered_edited < randmnew:
                   print("It's greater than that you twat")
         elif answered_edited > randmnew:
                   print("You're a little low shawty")
randomnumber=random.randint(1101) 而WIN==False: 提问=输入(“猜一个数字:”) 已回答\u编辑=int(提问) 随机数=int(随机数) 如果已回答_edited==randmnew: 打印(“你赢了!只是不要太激动 在你之前/n因为这是 任何孩子都只能玩一个简单的游戏。”) 韩元=真 elif回答_编辑randmnew: 打印(“你有点低了,肖蒂”)

您可以编写
randomnumber=random。randint(1101)
进入while循环,如果您想更改随机数,直到用户赢为止

听起来像是while循环的完美用例:

import random

random_number = random.randint(1, 101)

user_gussed_correctly = False

while not user_gussed_correctly:
    user_answer = int(input("Guess a number!: "))
    if user_answer < random_number:
        print("It's greater than that you twat!")
    elif user_answer > random_number:
        print("You're a little high shawty...")
    else:
        print("You've won! Just don't get too ahead of yourself\n"
              "since this was only a simple game any child could play.")
        user_gussed_correctly = True
替代变化:
  • 包装在函数中
    • 类型提示
      仅适用于
      python 3.5
  • 删除了单独的
    while循环
    变量
  • if-else
    替换为
    dict
    • 输出
      while循环中
      因此
      会使用每个新的
      猜测
    • dicts
      必须具有唯一的
      ,如果存在非唯一的
      ,则最后匹配的
      键将返回其
    • 输出
      的情况下,始终会有两个
      和一个
      ;与
      if-else
      条件一样,
      True
      是最重要的条件
代码: 使用可选间隔呼叫
游戏

这基本上只是一个变体。
Guess a number!: 50
It's greater than that you twat!
Guess a number!: 75
It's greater than that you twat!
Guess a number!: 82
It's greater than that you twat!
Guess a number!: 91
It's greater than that you twat!
Guess a number!: 96
You're a little high shawty...
Guess a number!: 93
You've won! Just don't get too ahead of yourself
since this was only a simple game any child could play.
import random

def game(low: int=1, high: int=100):

    guess, random_number = -1, random.randint(low, high+1)

    while guess != random_number:

        guess = int(input(f"Guess an integer from {low} to {high}!: "))

        output = {guess > random_number: "You're a little high, shawty...\n",
                  guess < random_number: "It's greater than that, you twat!\n",
                  guess == random_number: "You've won! Just don't get too ahead of yourself,\n"
                                          "since this is only a simple child's game."}

        print(output[True])
game()
game(1, 1000)