vincenzo0823 发表于 2017-2-25 22:53:39

优化一段代码以及其中的一个问题点。

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 。为何?
以及如何优化,感觉整体代码过于繁琐

冷笑寒风 发表于 2017-2-25 22:53:40

整段代码的问题蛮多的,还是要好好该改的,有几个问题我给你指一下,第一个循环条件错了 应该是 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()

谦虚求学 发表于 2017-3-1 13:49:23

冷笑寒风 发表于 2017-2-26 12:37
整段代码的问题蛮多的,还是要好好该改的,有几个问题我给你指一下,第一个循环条件错了 应该是 while 》0 ...

2楼回答了好多 问题   表示欣赏   本来是想赚几个鱼币 ,没办法 2楼吧好多问题都回答了 只能想楼主表达一下座椅个小游戏的尊敬,我想建议下楼主 吧小甲鱼老师讲的PYTHON,前面的元组 ,列表 ,集合,字典只是在好好看下
页: [1]
查看完整版本: 优化一段代码以及其中的一个问题点。