Python:while循环中的If语句
我写了一个程序,它不断滚动一个“num”边的骰子,直到它达到最大滚动,也就是你写为“num”的数字。然而,如果第一卷恰好是数字,程序不会像它应该说的那样说“你在你的数字上着陆了!”。这是密码Python:while循环中的If语句,python,while-loop,Python,While Loop,我写了一个程序,它不断滚动一个“num”边的骰子,直到它达到最大滚动,也就是你写为“num”的数字。然而,如果第一卷恰好是数字,程序不会像它应该说的那样说“你在你的数字上着陆了!”。这是密码 import random num = () #put the number here# i = random.randint(1,num) while i != num: print(i) print("Not lucky") i = random.rand
import random
num = () #put the number here#
i = random.randint(1,num)
while i != num:
print(i)
print("Not lucky")
i = random.randint(1,num)
if i == num:
print("You landed on your number!")
同样,如果滚动等于数字选择,我得到的是“进程结束,退出代码为0”,而不是我想要的文本。如何修复此问题?将最终打印放在
循环之外,因为您总是在那里着陆
num = 5 # put the number here#
i = random.randint(1, num)
while i != num:
print("Not lucky,", i, "isn't the one")
i = random.randint(1, num)
print("You landed on your number!")
将最后一次打印放在while
循环之外,因为您总是在那里着陆
num = 5 # put the number here#
i = random.randint(1, num)
while i != num:
print("Not lucky,", i, "isn't the one")
i = random.randint(1, num)
print("You landed on your number!")
如果是那样呢
import random
num = int(input('Put your number: '))
i = random.randint(1, num)
while True:
if i == num:
print("You landed on your number!")
print(num)
break
else:
print(i)
print("Not lucky")
i = random.randint(1, num)
如果是那样呢
import random
num = int(input('Put your number: '))
i = random.randint(1, num)
while True:
if i == num:
print("You landed on your number!")
print(num)
break
else:
print(i)
print("Not lucky")
i = random.randint(1, num)
您可以这样做:
import random
num = (2) #put the number here#
i = random.randint(1,num)
while i != num:
i = random.randint(1,num)
if i != num:
print(i, "Not lucky")
print(i, "You landed on your number!")
您可以这样做:
import random
num = (2) #put the number here#
i = random.randint(1,num)
while i != num:
i = random.randint(1,num)
if i != num:
print(i, "Not lucky")
print(i, "You landed on your number!")