|
35鱼币
- import random
- dice = []
- number = 3
- while number > 3:
- dice = random.randrange(1.7)
- dice.append(dice)
- number -= 1
- total = sum(dice)
- if 11 <= total <= 18:
- answer = 'Big'
- else:
- answer = 'Small'
- def start_game():
- money = 1000
- while money > 0:
- print('<<<Game Starts!>>>')
- guess = input('Big or Small : ')
- while guess != 'Big' or 'Small':
- guess = input('Invalid Words,Please write again')
- money_stake = int(input('How much you wanna bet ? '))
- print('<<<Roll The Dice!>>>')
- if guess == answer:
- print ('The points is ',total,'You Win')
- gained = money
- money = gained + money
- print ('You gained',gained,',you have ',money,'now')
- else:
- print ('The points is ',total,'You Lose')
- gained = - money_stake
- money = gained + money
- print('You lost',gained,',you have ',money,'now')
- print('Game Over')
- start_game()
复制代码
如上图代码(押注游戏)
其中的pointe 打印出来总是 0 。为何?
以及如何优化,感觉整体代码过于繁琐
整段代码的问题蛮多的,还是要好好该改的,有几个问题我给你指一下,第一个循环条件错了 应该是 while 》0: 第二点是 random函数调用错了 应该是random.randrange(1,7)是逗号。在一个下面while guess != 'Big' or 'Small':判断条件错了,应该是while guess != 'Big' or guess != 'Small': or前后连接的应该是两个判断,你写的or前面的是判断 ,后面是个字符串,会出现若不是big就恒ture,然后我实现了一下代码如下,希望对你有帮助~
import random
class game :
def __init__ (self):
self.dice = []
self.answer = 'null'
self.guess = 'null'
self.total = 0
def sum (self):
self.total = sum(self.dice)
def roling(self):
number = 3
while number > 0:
n_dice = random.randrange(1,7)
self.dice.append(n_dice)
number -= 1
def juge (self):
total = sum(self.dice)
if 11 <= total <= 18:
self.answer = 'Big'
else:
self.answer = 'Small'
def input (self):
guess = input('Big or Small : ')
if guess == 'Big' or guess =='Small':
print ('你选择的是'+guess)
self.guess = guess
else:
print('Invalid Words,Please write again')
self.input()
def g_main (self):
money =1000
while money >0:
print('<<<Game Starts!>>>')
self.input()
money_stake = int(input('How much you wanna bet ? '))
print('<<<Roll The Dice!>>>')
self.roling()
self.sum()
self.juge()
if self.guess == self.answer:
print ('The points is ',self.total,'You Win')
gained = money_stake
money = gained + money
print ('You gained',gained,',you have ',money,'now')
else:
print ('The points is ',self.total,'You Lose')
gained = - money_stake
money = gained + money
print('You lost',gained,',you have ',money,'now')
print('Game over')
if __name__=='__main__':
a = game()
a.g_main()
|
最佳答案
查看完整内容
整段代码的问题蛮多的,还是要好好该改的,有几个问题我给你指一下,第一个循环条件错了 应该是 while 》0: 第二点是 random函数调用错了 应该是random.randrange(1,7)是逗号。在一个下面while guess != 'Big' or 'Small':判断条件错了,应该是while guess != 'Big' or guess != 'Small': or前后连接的应该是两个判断,你写的or前面的是判断 ,后面是个字符串,会出现若不是big就恒ture,然后我实现了一下代码如下,希望对你有 ...
|