【问题标题】:Formatting numbers not raising error in Turtle Window格式化数字不会在 Turtle Window 中引发错误
【发布时间】:2021-11-19 12:41:08
【问题描述】:

我有这个 Turtle Graphic,它创建了一个消息框,您可以在其中插入一个整数。 这一直有效,直到我决定添加一种为每个第三位值添加逗号的格式。 当它发生时,它会引发一个 ValueError。

这是它应该做的:

bettedc = 10000

print(f'User betted {bettedc}$')
---------------------------------
User betted 10,000 

这是错误中涉及的代码。

import turtle
import json

with open("data.json") as file:
    data = json.load(file)


def betcurrency():
 try:
    global bettedc

    bettedc = turtle.textinput('Bet money.',
    f'You have {data["money"]:,}$. Remember, you can only bet money you have.')

    if int(bettedc) > data["money"]:
        print("User tried to bet what they didn't have.")
        betcurrency()
    elif int(bettedc) < 0:
        print('User tried to bet a negative amount.')
        betcurrency()
    else:
        print(f'User betted {bettedc:,}$')  # This line is where the error happens. It can be avoided if I remove :,
 except ValueError:
    print("User wrote word or a float number.")
    betcurrency()

您可能不需要这个,但这里是 .json 文件。

{"money": 100000}

【问题讨论】:

    标签: python json formatting python-turtle


    【解决方案1】:

    我在这里犯的菜鸟错误...我忘记了bettedc 是一个字符串。

    解决方案在else: 语句中。

    print(f'User betted {int(bettedc):,}$')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 2019-04-04
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多