gpt4 book ai didi

python - 类型错误 : not all arguments converted during string formatting python 3

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:15 26 4
gpt4 key购买 nike

计算机科学类(class)的弹道计算器。我不明白为什么它一直显示以下错误消息:

travel_time = range % velocity
TypeError: not all arguments converted during string formatting


print ("Welcome to the Laird Industries Ballistic Calculator")
range = raw_input("What is the approximate distance to your target? (m)")
velocity = raw_input("What is the muzzle velocity of the projectile? ")

def time_to_target():
travel_time = range % velocity
print "Travel duration {0}".format(travel_time)
#
time_to_target()
#

感谢您提供信息。固定代码:

range_to_target = raw_input("What is the approximate distance to your target? (m)")
velocity = raw_input("What is the muzzle velocity of the projectile? ")

def time_to_target():
travel_time = float(range_to_target) / float(velocity)
print "Travel duration {0}".format(travel_time)
#
time_to_target()
#

最佳答案

因为 range 是一个字符串(你从 input() 得到的)而 % 在 Python 中对于字符串是一个特殊的格式化运算符.您只需要转换为数字数据类型,如下所示:

travel_time = float(range) % float(velocity)

关于python - 类型错误 : not all arguments converted during string formatting python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com