我这里有这个简短的代码。但它不是使用python在wing ide 4.1中打印,因为它是一个无限循环。我可以添加什么或如何修复它以便打印的任何想法?
import random
coins = 1000
wager = 2000
while ((coins>0) and (wager!= 0)):
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print x,
print y,
print z
答案 0 :(得分:2)
您的代码永远不会改变coins
或 wager
,因此while
条件总是为真:
while ((coins>0) and (wager!= 0)):
# code that doesn't touch coins or wager
也许您打算从x
或y
中减去z
,coins
或wager
中的一个?
为什么代码不能在Wing IDE中打印;这完全取决于你的缩进。如果print
语句不是循环的一部分,则永远不会到达并且永远不会执行它们。尝试创建一个无法无限运行的循环。
答案 1 :(得分:1)
您发布的代码不会做任何事情,只会选择3个伪随机数字直到时间结束。你必须添加一些赢/输条件。截至目前,x,y和z只是数字。如果你想制作一个赌博游戏,你必须添加一些胜利条件,如:
if x + y + z > 10
只是一个例子,但你的程序需要能够判断玩家是否赢了。然后它需要改变球员的总金额并要求新的赌注。您也可能想要添加逻辑以确保玩家不能下注超过他们的赌注。
import random
coins = 1000
wager = 0
while True: #main loop
print('you have {} coins'.format(coins))
if coins == 0: #stops the game if the player is out of money
print('You are out of money! Scram, deadbeat!')
break
while wager > coins or wager == 0: #loops until player enters a non-zero wager that is less then the total amount of coins
wager = int(input('Please enter your bet (enter -1 to exit): '))
if wager < 0: # exits the game if the player enters a negative
break
print('All bets are in!')
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print(x,y,z) #displays all the random ints
if x + y +z > 10: #victory condition, adds coins for win
print('You win! You won {} coins.'.format(wager))
coins += wager
else: #loss and deduct coins
print('You lost! You lose {} coins'.format(wager))
coins -= wager
wager = 0 # sets wager back to 0 so our while loop for the wager validation will work
答案 2 :(得分:0)
因为你的while循环中没有修改你的破坏条件。
在您的情况下,中断条件是wager is not 0
和coins > 0
,所以您必须修改代码中的coins
或wager
变量< / strong>,例如
import random
coins = 1000; wager = 2000
while coins > 0 and wager is not 0:
x,y,z = [random.randint(0,10)]*3
print x,y,z
wager-= 1000
coins-= 100
由于@martijn缩进在python中非常重要,请参阅http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Indentation:
for _ in range(10):
x = 'hello world'
print x
[OUT]:
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
当print x
没有缩进时:
for _ in range(10):
x = 'hello world'
print x
[OUT]:
hello world