我试图在壁架随机机会游戏上编码一种行走,用户会输入他们想要下注的一定金额,然后根据他们将采取多少步骤,他们将生活或从壁架上掉下来。到目前为止,代码是FAR完成但我遇到了一个问题,我想知道是否有人可以帮我修复它。
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()
我该如何解决?
答案 0 :(得分:1)
问题在于这一行:
if strgold <= PlayerIG.gold:
这里你要比较一个字符串和一个整数。这是不可能的,您必须先将字符串转换为整数:
if int(strgold) <= PlayerIG.gold:
我没有检查你的其余代码,但我怀疑你在其他地方也可能也有类似的错误。