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)