鱼C论坛

 找回密码
 立即注册
查看: 3468|回复: 2

[已解决]优化一段代码以及其中的一个问题点。

[复制链接]
发表于 2017-2-25 22:53:39 | 显示全部楼层 |阅读模式
35鱼币
  1. import random
  2. dice = []
  3. number = 3
  4. while number > 3:
  5.      dice = random.randrange(1.7)
  6.      dice.append(dice)
  7.      number -= 1
  8. total = sum(dice)
  9. if 11 <= total  <= 18:
  10.      answer = 'Big'
  11. else:
  12.      answer = 'Small'
  13. def start_game():
  14.      money = 1000
  15.      while money > 0:
  16.           print('<<<Game Starts!>>>')
  17.           guess = input('Big or Small : ')
  18.           while guess != 'Big' or 'Small':
  19.                guess = input('Invalid Words,Please write again')
  20.           money_stake = int(input('How much you wanna bet ? '))
  21.           print('<<<Roll The Dice!>>>')
  22.           if guess == answer:
  23.                print ('The points is ',total,'You Win')
  24.                gained = money
  25.                money = gained + money
  26.                print ('You gained',gained,',you have ',money,'now')
  27.           else:
  28.                print ('The points is ',total,'You Lose')
  29.                gained = - money_stake
  30.                money = gained + money
  31.                print('You lost',gained,',you have ',money,'now')
  32.      print('Game Over')
  33. 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()

最佳答案

查看完整内容

整段代码的问题蛮多的,还是要好好该改的,有几个问题我给你指一下,第一个循环条件错了 应该是 while 》0: 第二点是 random函数调用错了 应该是random.randrange(1,7)是逗号。在一个下面while guess != 'Big' or 'Small':判断条件错了,应该是while guess != 'Big' or guess != 'Small': or前后连接的应该是两个判断,你写的or前面的是判断 ,后面是个字符串,会出现若不是big就恒ture,然后我实现了一下代码如下,希望对你有 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

2  楼回答了好多 问题   表示欣赏   本来是想赚几个鱼币 ,没办法 2楼吧好多问题都回答了 只能想楼主表达一下座椅个小游戏的尊敬,我想建议下楼主 吧小甲鱼老师讲的PYTHON,  前面的元组 ,列表 ,集合,字典只是在好好看下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表