2017-03-05 119 views
1

我正在尝试编写一种走在随机偶数游戏机上的游戏,用户会输入一定数量他们想打赌,然后根据他们要采取多少步骤,他们会要么住或者从窗台上掉下来。到目前为止的代码是FAR完成,但我遇到了一个问题,我想知道如果有人可以帮我修复它。我的python游戏代码不能正常工作

import time 
import random 


class Player(): 
    def __init__(self,name): 
     self.name = name 
     self.luck = 2 
     self.gold = 10 

def main(): 
    print("Hello what is your name?") 
    option = input("--> ") 
    global PlayerIG 
    PlayerIG = Player(option) 
    start1() 

def start1(): 
    print("Name: {}".format(PlayerIG.name)) 
    print("Luck: {}".format(PlayerIG.luck)) 
    print("Gold: {}".format(PlayerIG.gold)) 
    inputgold() 

def inputgold(): 
    print("Please input how much gold you would like to play with") 
    goldinput = input("--> ") 
    strgold = str(goldinput) 
    print("You inputted {}".format(strgold)) 
    if strgold <= PlayerIG.gold: 
     print("You don't have enough gold") 
     inputgold() 
    else: 
     print("Get ready to start!") 
    ledge() 

def ledge(): 
    print("You are standing on a ledge with an unknown length") 
    time.sleep(1) 
    choice = input("How many steps do you want to take forward? Between 1-100") 
    if choice == step1: 
     print("You have fallen off the ledge") 
     PlayerIG.gold -= goldinput 
     print("Gold: ".format(PlayerIG.gold)) 
    elif choice == step2: 
     print("You...") 
     time.sleep(1) 
     print("Didn't fall off the ledge!") 
     PlayerIG.gold*1.2 
     print("Gold: ".format(PlayerIG.gold)) 
    else: 
     print("You slipped off the ledge and face planted onto the side walk") 
     PlayerIG.gold -= goldinput 
     print("Gold: ".format(PlayerIG.gold)) 

def steps(): 
    step1 = random.randint(10,30) 
    step2 = random.randint(30,50) 
    step3 = random.randint(50,100) 

main() 

当我运行它是说:

if strgold <= PlayerIG.gold: TypeError: unorderable types: str() <= int()

我怎样才能解决呢?

回答

1

的问题是这一行:

if strgold <= PlayerIG.gold: 

这里你比较整数的字符串。这是不可能的,你必须将字符串转换为整数第一:

if int(strgold) <= PlayerIG.gold: 

我没有检查你的代码的其余部分,但我怀疑你可能也有类似的错误在其他地方了。