如何从分数中减去Python 3.4?

时间:2014-11-28 21:27:13

标签: python

我正在用Python制作一个小型赌博游戏,你开始时花500美元,但我想在你使用它时减去你的钱。我有钱+ = irand(irand是你通过赌博赚的钱),但我不知道如何从字符串钱中减去钱。有帮助吗?谢谢。

这是我的代码:

import sys
import os
import time
import math
from random import randrange, uniform
#CLS
def cls():
    os.system('cls')
#Main Game
def casino():
    money = 500
    print ("You have $500 dollars to start with.")
    time.sleep(3)
    while True:
        bet = input ("How much money do you wish to bet?: ")
        if bet >= '1' or bet <= '50':
            irand = randrange(0, 101)   
            print ("Your earnings are $%s" % irand + "!")
            time.sleep(3)
            money += irand
            print ("You now have $%s" % money + "!")
            time.sleep(4)
            cls()
            continue
        if bet >= '51' or bet <= '100':
            irand = randrange(0, 201)
            print ("Your earnings are $%s" % irand + "!")
            time.sleep(3)
            money += irand
            print ("You now have $%s" % money + "!")
            time.sleep(4)
            cls()
            continue
        if bet >= '101' or bet <= '150':
            irand = randrange(0, 301)
            print ("Your earnings are $%s" % irand + "!")
            time.sleep(3)
            money += irand
            print ("You now have $%s" % money + "!")
            time.sleep(4)
            cls()
            continue
        if bet >= '151' or bet <= '200':
            irand = randrange(0, 401)
            print ("Your earnings are $%s" % irand + "!")
            time.sleep(3)
            money += irand
            print ("You now have $%s" % money + "!")
            time.sleep(4)
            cls()
            continue
        if bet >= '201' or bet <= '250':
            irand = randrange(0, 501)
            print ("Your earnings are $%s" % irand + "!")
            time.sleep(3)
            money += irand
            print ("You now have $%s" % money + "!")
            time.sleep(4)
            cls()
            continue
#Intro to Game - Optional
def intro():
    print ("CMD Casino is a small CMD game that simulates a gambling game.")
    time.sleep(4)
    print ("Just type in a money value and the game will determine if you make or lose out on your bet.")
    time.sleep(4)
    cls()
#Main Code
def main():
    print ("Please select an option when prompted!")
    while True:
        time.sleep(0.5)
        print ("[1] Casino")
        time.sleep(0.5)
        print ("[2] How-To")
        time.sleep(0.5)
        print ("[3] Exit")
        time.sleep(1)
        menu = input ("Please Choose: ")
        cls()
        time.sleep(1)
        if menu == '1':
            casino()
        if menu == '2':
            intro()
        if menu == '3':
            print ("Exiting the game...")
            time.sleep(2)
            break
            SystemExit

#Launch Code
if __name__ == "__main__":
    print ("CMD Casino")
    time.sleep(2)
    print ("By: Oiestin")
    time.sleep(2)
    print ("In hand with: oysterDev")
    time.sleep(4)
    cls()
    print ("Version Beta 1.0.0")
    time.sleep(4)
    cls()
    main()

1 个答案:

答案 0 :(得分:1)

您的代码不起作用,因为您尝试使用>=等数学运算符来比较字符串值,这将导致意外(对您而言)结果。相反,所有货币值都应该是整数或浮点数,并且您应该将input()值转换为整数或浮点数,以便数学运算正常。

只是为了说明:

>>> "20" > "1000"  # because 50 (ord("2")) is > 49 (ord("1"))
True