【问题标题】:Having issues with numbers being printed打印数字时遇到问题
【发布时间】:2018-10-16 17:45:13
【问题描述】:

我正在开发一个投币游戏,它将您输入的价值添加到您的余额中

之前的代码片段:

deposit = input("Enter how much to deposit")

money = file1.read()

resultmonecoin = int(money)-int(deposit)

测试 Tails 是否正确的主要代码

elif flipresults == "Tails":
                    print("You flip the coin")
                    time.sleep(2)
                    print("It's tails")
                    if HORT.upper() == "T" or "t":
                        wincoin = int(deposit)*2
                        print("You won",wincoin)
                        howmuchwin = wincoin+money
                        print("Total:",int(howmuchwin))
                    else:
                        print("You lost that one. You now have",resultmonecoin)
                        game()
                else:
                    print("This input wasn't understood!")
                    flip()

注意: money 变量类似于 500200

问题:

[H] 领先或 [T] 落后 T

你掷硬币

这是尾巴

你赢了 468

Traceback (most recent call last):
File "C:\Users\yeet\Desktop\Casino.py", line 40, in flip
    howmuchwin = wincoin+money
TypeError: unsupported operand type(s) for +: 'int' and 'str

【问题讨论】:

  • "货币变量类似于 500 或 200"。是500,还是"500"?非常重要的区别。
  • 500 没有任何引号
  • 如果您说“当我执行 print(money) 时,它会显示 500,这并不能真正回答我的问题。500"500" 看起来像 500 时你打印它们。
  • 所以钱存储在一个 TXT 文件中,我输入:money = file1.read() 在 txt 文件中除了显示的数字 500 之外什么都没有

标签: python string random int coin-flipping


【解决方案1】:

正如错误所说,您不能将字符串和整数连接在一起。看起来您的文件正在读取字符串,因此您需要将其转换为像 int(money) 这样的整数才能执行数学运算

【讨论】:

  • 谢谢,我看了一点,结果一切都坏了,因为文件没有被读取为整数!
猜你喜欢
  • 2016-06-25
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2021-02-08
  • 2021-12-13
相关资源
最近更新 更多