Python程序错误

Python程序错误,python,python-2.7,Python,Python 2.7,我试着做一个简单的“投资游戏”。但出于某种原因,可变现金在“投资”后仍表示为1000。我也想让这场比赛持续下去。喜欢的玩家可以继续玩,并获得/失去现金。节目在下面!谢谢 import sys import random print "INVEST" cash = 1000 highlow = ["h", "l"] percentrand = random.randint(1,99) percentup = percentrand/100 + 1 percentdown = percentrand

我试着做一个简单的“投资游戏”。但出于某种原因,可变现金在“投资”后仍表示为1000。我也想让这场比赛持续下去。喜欢的玩家可以继续玩,并获得/失去现金。节目在下面!谢谢

import sys
import random
print "INVEST"
cash = 1000
highlow = ["h", "l"]
percentrand = random.randint(1,99)
percentup = percentrand/100 + 1
percentdown = percentrand/100 - 1
randomhighlow = random.choice(highlow)
print "You have 1000$ on you now."
investquit = raw_input("Invest or quit?")
if investquit.lower() == "quit":
    quit()
elif investquit.lower() == "invest":
    if randomhighlow == "h":
    cash == cash*percentup
    print str(cash) + ",up," + str(percentrand) + "%"
if randomhighlow == "l":
    cash == cash*percentdown
    print str(cash) + ",down," + str(percentrand) + "%"

您没有循环来多次运行程序。此外,在Python2.7中,将两个整数除以将产生另一个整数,而不是浮点。这就是你的主要问题所在,因为这导致上升或下降百分比始终为1

所以你应该这样做:

percentrand = float(random.randint(1,99))
percentup = percentrand/100.0 + 1
percentdown = percentrand/100.0 - 1
randomhighlow = random.choice(highlow)

您没有循环来多次运行程序。此外,在Python2.7中,将两个整数除以将产生另一个整数,而不是浮点。这就是你的主要问题所在,因为这导致上升或下降百分比始终为1

所以你应该这样做:

percentrand = float(random.randint(1,99))
percentup = percentrand/100.0 + 1
percentdown = percentrand/100.0 - 1
randomhighlow = random.choice(highlow)

双等于
=
是比较运算符,而单等于
=
是赋值运算符

在您的情况下,您需要更新现金价值

cash = cash * percentup
(并相应降低百分比)

要让游戏无限地玩,或者直到某个特定条件(即cash>0),您可以在while循环中围绕整个内容,例如

while cash > 0:
  percentrand = float(random.randint(1,99))
  [.. rest of code ...]

编辑:正如Ryan正确地提到的,您希望
percentrand=float(random.randint(1,99))
确保除法结果不是整数。

Double equals
=
是一个比较运算符,而单个equals
=
是赋值

在您的情况下,您需要更新现金价值

cash = cash * percentup
(并相应降低百分比)

要让游戏无限地玩,或者直到某个特定条件(即cash>0),您可以在while循环中围绕整个内容,例如

while cash > 0:
  percentrand = float(random.randint(1,99))
  [.. rest of code ...]

编辑:正如Ryan正确地提到的,您需要
percentrand=float(random.randint(1,99))
确保除法结果不是整数。

您有几个问题。其他答案和评论涵盖了其中的大部分,但我将把它们合并成一个答案

首先,当您应该使用浮点除法时,您正在使用整数除法。这在Python3.x中可以工作,但由于您将其标记为2.7,因此它有所不同:

percentup = percentrand/100.0 + 1
与“向下”相同,只是从1中减去了1:

那么您使用了错误的运算符来分配现金:

cash = cash*percentup
并且在你发布代码时,代码中有不正确的缩进

最后,您需要一个循环来继续播放:

while True:
这似乎有效:

import sys
import random
print "INVEST"
cash = 1000
highlow = ["h", "l"]

while True:
    percentrand = random.randint(1,99)
    percentup = percentrand/100.0 + 1
    percentdown = 1 - percentrand/100.0
    randomhighlow = random.choice(highlow)
    print "You have $" + str(cash) + " on you now."
    investquit = raw_input("Invest or quit?")
    if investquit.lower() == "quit":
        break
    elif investquit.lower() == "invest":
        if randomhighlow == "h":
            cash = cash*percentup
            print str(cash) + ",up," + str(percentrand) + "%"
        if randomhighlow == "l":
            cash = cash*percentdown
            print str(cash) + ",down," + str(percentrand) + "%"
print 'Thanks for playing!'

你有几个问题。其他答案和评论涵盖了其中的大部分,但我将把它们合并成一个答案

首先,当您应该使用浮点除法时,您正在使用整数除法。这在Python3.x中可以工作,但由于您将其标记为2.7,因此它有所不同:

percentup = percentrand/100.0 + 1
与“向下”相同,只是从1中减去了1:

那么您使用了错误的运算符来分配现金:

cash = cash*percentup
并且在你发布代码时,代码中有不正确的缩进

最后,您需要一个循环来继续播放:

while True:
这似乎有效:

import sys
import random
print "INVEST"
cash = 1000
highlow = ["h", "l"]

while True:
    percentrand = random.randint(1,99)
    percentup = percentrand/100.0 + 1
    percentdown = 1 - percentrand/100.0
    randomhighlow = random.choice(highlow)
    print "You have $" + str(cash) + " on you now."
    investquit = raw_input("Invest or quit?")
    if investquit.lower() == "quit":
        break
    elif investquit.lower() == "invest":
        if randomhighlow == "h":
            cash = cash*percentup
            print str(cash) + ",up," + str(percentrand) + "%"
        if randomhighlow == "l":
            cash = cash*percentdown
            print str(cash) + ",down," + str(percentrand) + "%"
print 'Thanks for playing!'

打字错误<代码>=用于比较<代码>=用于分配。投票结束打字错误。请转到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题<代码>=用于比较<代码>=用于分配。投票结束打字错误。请转到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。