好吧,我已经做了几天了,但我自己无法修复它。
Python3和下面的代码片段。我的问题是变量类型和用户输入。这个代码是一个更大代码的一部分,我希望用户(大多数是我(能够使用有意义的变量名称(地球质量的"m_earth"和类似的量(作为计算"m_"的输入;F1";,对于其他人,我想使用。。。。如果…我可以先把这个部分做好。
我尝试过使用";浮动";以及";int";它真的只是造成了其他我无法修复的错误。
下面包含的代码是使用此代码时会发生的情况。
任何人都有一些建议。。。请
感谢
G = float(6.674E-11)
# All masses are in Kg
m_earth = 5.972E24
m_moon = 7.345E22
m1 = input('Enter the first mass ')
print(m1, 'Kg')
m2 = input('Enter the second mass ')
print(m2, 'Kg')
r = input('Enter the center-to-center distance from mass m1 to mass m2 ')
F1 = G * m1 * m2 / r ** 2
print('The gravitational force is ', F1, "N")
Enter the first mass m_earth
m_earth Kg
Enter the second mass m_moon
m_moon Kg
Enter the center-to-center distance from mass m1 to mass m2 3E12
Traceback (most recent call last):
File "C:/Users/xyz/PycharmProjects/My Python Programs/try it.py", line 15, in <module>
F1 = G * m_earth * m_moon / r ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
进程结束,退出代码为1
由于input()
总是返回一个字符串,因此会出现此错误。不能向int
s或float
s添加任意字符串的原因是,在大多数情况下,这是没有意义的。
考虑3 + 'Hello'
。你预计结果会是什么?
那么我们该如何处理呢?我们将其转换为python所理解的允许执行算术函数的东西。在python中,这些类型是int
(整数(和float
小数。为了论证起见,还有很多其他的,但你不需要它们作为你的榜样。
由于我们不能保证用户已经输入了数字,我们需要进行某种类型的检查以确保他们输入了数字。
这就是我们可以使用try
/except
块的地方。
G = float(6.674E-11)
# All masses are in Kg
m_earth = 5.972E24
m_moon = 7.345E22
m1 = input('Enter the first mass ')
print(m1, 'Kg')
m2 = input('Enter the second mass ')
print(m2, 'Kg')
r = input('Enter the center-to-center distance from mass m1 to mass m2 ')
# any Error that occurs in the try block
# will hand control to the except block
# if the type of Exception in except is
# the type of error that occurred
try:
# here we try to convert the variable m1 of type str
# to a variable of type float
# since float() "returns" a float and does not convert in place
# we need to catch the output it gives into the same variable
m1 = float(m1)
m2 = float(m2)
r = float(r)
F1 = G * m1 * m2 / r ** 2
print('The gravitational force is ', F1, "N")
# any conversion error here will result in a ValueError
# We can catch that specific error and alert the user they
# mis-entered some data
except ValueError as v:
print('Some of your input is not valid.')
但是您希望能够指定变量的名称。为此,您可以使用pythondict
。dict
将键映射到某个值。键可以是多种类型,但最重要的是,它们可以是str,即input()
返回的类型。在这种情况下,它可以是m_earth
。
# we need sys to do an early exit in the program
# if the data entered is not a key in the dict
import sys
G = float(6.674E-11)
# All masses are in Kg
my_dict = {'m_earth': 5.972E24,
'm_moon': 7.345E22}
m1 = input('Enter the first mass ')
# this will raise a KeyError
# if the key is not in your dict
try:
m1 = my_dict[m1]
except KeyError as k:
sys.exit('That name is invalid.')
print(m1, 'Kg')
m2 = input('Enter the second mass ')
try:
m2 = my_dict[m2]
except KeyError as k:
sys.exit('That name is invalid.')
r = input('Enter the center-to-center distance from mass m1 to mass m2 ')
try:
r = float(r)
F1 = G * m1 * m2 / r ** 2
print('The gravitational force is ', F1, "N")
except ValueError as v:
print('Some of your input is not valid.')