嘿,伙计们,这是我正在做的一款游戏。据我所知,错误在于,当你第一次进入银行并提取现金以支付罚款时,但下一次它读到手头时,它会推我 |回溯(最近一次调用):文件 " 第 1 行,在类型错误中:无法将 'int' 对象隐式转换为 str|无论如何,似乎一旦您使用 onhand = int(onhand) + int(amount2),它就会更改字符串,为什么无法在打印中显示("~ On Hand:" + onhand)谢谢大家
开始
import time
energy = "10"
int(energy)
money = "500"
onhand = "10"
int(money)
int(onhand)
播放器创建
print ("Create Your Player!")
print (".......................")
name = input ("What's Your Name:")
print ('Okay Gotcha ' + name)
time.sleep (2)
print ('')
age = input ("What's Your Age Sir?:")
print ("Okay Gotcha you're " + age)
time.sleep (2)
print ('')
hometown = input ("Where Are You From?:")
print ("It's Nice Over In " + hometown + ' huh')
time.sleep (2)
print ('')
print ("Well im done here. Have a nice trip!")
time.sleep (5)
print ('')
#PLAYER CREATION
第一个菜单菜单第一个
count = 0
while count < 1000:
print (' ')
print (' ')
print (' ')
print ('You walk into your room what do you do?')
print ("~~~~~~~~~energy~~| "+ energy + '|~~Name:~~|' + name)
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
time.sleep (1)
print ("~ 1 | Check Bank Account ~")
time.sleep (1)
print ("~ 2 | Go Eat At Tiffany's ~")
time.sleep (1)
print ("~ 3 | Tour Jurrasic Park ~")
time.sleep (1)
print ("~ 4 | Work +$7 ~")
time.sleep (1)
print ("~ 5 | Sleep +10 energy ~")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
choice1 = input ("Enter Choice:")
第一个菜单菜单第一个
银行 银行银行
if int(choice1) == 1:
print (' ')
print ("~~~~~~~~~energy~~| "+ energy + '|~~Name:~~|' + name)
print ("~ First Dodo National Bank ~")
time.sleep (1)
print ("~ Name: " + name)
time.sleep (1)
print ("~ Age: " + age)
time.sleep (1)
print ("~ Bank #: 12342215")
time.sleep (1)
print ("~ Balance: " + money)
time.sleep (1)
print ("~ On Hand: " + onhand)
time.sleep (3)
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print (' ')
print ('1 | Deposit')
print ('2 | Withdraw')
print ('3 | Leave')
choice2 = input ("Enter Choice:")
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
if int(choice2) == 1:
amount1 = input ('Enter Amount:')
if int(amount1) > int(onhand):
print ('You Dont Have That Much Money On Hand.')
elif int(amount1) <= int(onhand):
money = int(money) + int(amount1)
count += 1
elif int(choice2) == 2:
amount2 = input ('Enter Amount:')
if int(amount2) > int(money):
print ('You Dont Have That Money In The Bank.')
elif int(amount2) <= int(money):
onhand = int(onhand) + int(amount2)
count += 1
elif int(choice2) == 3:
count += 1
银行 银行 银行
完整代码
import time
energy = "10"
int(energy)
money = "500"
onhand = "10"
int(money)
int(onhand)
#PLAYER CREATION
print ("Create Your Player!")
print (".......................")
name = input ("What's Your Name:")
print ('Okay Gotcha ' + name)
time.sleep (2)
print ('')
age = input ("What's Your Age Sir?:")
print ("Okay Gotcha you're " + age)
time.sleep (2)
print ('')
hometown = input ("Where Are You From?:")
print ("It's Nice Over In " + hometown + ' huh')
time.sleep (2)
print ('')
print ("Well im done here. Have a nice trip!")
time.sleep (5)
print ('')
#PLAYER CREATION
#INTRO
print ('You Just Flew In From Your Home Town Of ' + hometown + " You're Excited About Your New Job In Jurassic Park")
time.sleep (4)
print ('')
print ('Bob: Well ' + name + ' Welcome To JURASSIC PARK!')
time.sleep (2)
print ('')
print ("Bob: So you're the new guy huh? Well I think you'll like it here.")
time.sleep (4)
print ('')
print ("Bob: Anyways your job is at the souvenir stand ")
time.sleep (3)
print ('')
print ("Bob: You can also get promoted so you won't be stuck selling rubber dinos forever don't worry")
time.sleep (3)
print (" ")
print ("Bob: ah here we are! *You arrive at a hotel inn called ~Le Dodo Pin~* Bob: Well good luck and if you")
print (" ")
time.sleep (2)
print ("get lost there are maps everywhere, if you're looking for a place to eat Tiffanys is one of the best")
print (" ")
time.sleep (2)
print ("food joints around here, anyways I have to go good luck! Don't let the dinos bite haha.")
time.sleep (4)
#INTRO
# 1ST MENU MENU MENU MENU MENU 1ST
count = 0
while count < 1000:
print (' ')
print (' ')
print (' ')
print ('You walk into your room what do you do?')
print ("~~~~~~~~~energy~~| "+ energy + '|~~Name:~~|' + name)
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
time.sleep (1)
print ("~ 1 | Check Bank Account ~")
time.sleep (1)
print ("~ 2 | Go Eat At Tiffany's ~")
time.sleep (1)
print ("~ 3 | Tour Jurrasic Park ~")
time.sleep (1)
print ("~ 4 | Work +$7 ~")
time.sleep (1)
print ("~ 5 | Sleep +10 energy ~")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
choice1 = input ("Enter Choice:")
# 1ST MENU MENU MENU MENU MENU 1ST
#BANK BANK BANK
if int(choice1) == 1:
print (' ')
print ("~~~~~~~~~energy~~| "+ energy + '|~~Name:~~|' + name)
print ("~ First Dodo National Bank ~")
time.sleep (1)
print ("~ Name: " + name)
time.sleep (1)
print ("~ Age: " + age)
time.sleep (1)
print ("~ Bank #: 12342215")
time.sleep (1)
print ("~ Balance: " + money)
time.sleep (1)
print ("~ On Hand: " + onhand)
time.sleep (3)
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print (' ')
print ('1 | Deposit')
print ('2 | Withdraw')
print ('3 | Leave')
choice2 = input ("Enter Choice:")
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
if int(choice2) == 1:
amount1 = input ('Enter Amount:')
if int(amount1) > int(onhand):
print ('You Dont Have That Much Money On Hand.')
elif int(amount1) <= int(onhand):
money = int(money) + int(amount1)
count += 1
elif int(choice2) == 2:
amount2 = input ('Enter Amount:')
if int(amount2) > int(money):
print ('You Dont Have That Money In The Bank.')
elif int(amount2) <= int(money):
onhand = int(onhand) + int(amount2)
count += 1
elif int(choice2) == 3:
count += 1
#BANK BANK BANK
如果不能隐式完成,则显式地这样做
print ("~ On Hand: " + str(onhand))
你给打印的参数"~ On Hand: " + onhand
是一个字符串串联。这两个部分都必须是字符串,以便能够将两者连接在一起。完成onhand = int(onhand) + int(amount2)
后onhand
是一个整数,而不是字符串。
主要方法可以进行字符串格式化。我发现最简单的方法是 .format()
方法,它用给定的参数替换字符串中的{}
format
:
"~ On Hand: {}".format(onhand)
您还可以使用 % 运算符,例如:
" ~ On Hand: %s" % onhand
顺便说一下,这些:
int(money)
int(onhand)
由于它们自己在线,因此不要做任何有用的事情。