不改变变量
Doesn't change variable
我正在学习 python,我正在尝试制作另一个计算器。当我尝试 运行 时,前几个命令有效,但当我到达第 6 行时,它说:
TypeError: can only concatenate str (not "int") to str
代码在这里:
if user_input==' squares':
first_number=input(str(inp_words))
second_number=input(str(sec_inp_words))
f_num=str(first_number)
s_num=str(second_number)
answer=int(first_number)**int(second_number)
print('the answer to '+str(f_num)+'to the power of'+str(s_num)+'is'+answer)
print(str(words))
sys.exit()
这是一种使用格式化字符串的方法:
print(f'the answer to {f_num} to the power of {s_num} is {answer}.')
当您在字符串前添加 f 或 F 时,该字符串称为格式化字符串,就像您在字符串末尾添加 .format() 一样。 使用格式化字符串,您无需担心类型转换。
在花括号中,您可以在其中放置任何有效的 python 表达式,例如函数,因为 f 字符串是在运行时求值的。
@AnnZen 的回答应该可以解决您眼前的问题 (+1)。但是你似乎有一个更大的问题来处理 int
和 str
并记住哪个变量是哪个:
second_number=input(str(sec_inp_words))
s_num=str(second_number)
answer=int(first_number)**int(second_number)
print('the answer to '+str(f_num)+'to the power of'+str(s_num)+'is'+answer)
因为您多次将字符串转换为字符串!一个更好地处理这个问题的方法可能是在变量名称中包含变量类型:
inp_words_str = "Please enter the first number: "
sec_inp_words_str = "Please enter the second number: "
words_str = "Goodbye!"
# ...
if user_input_str == 'squares':
first_number_str = input(inp_words_str)
second_number_str = input(sec_inp_words_str)
first_number_int = int(first_number_str)
second_number_int = int(second_number_str)
answer_int = first_number_int ** second_number_int
print(f'The answer to {first_number_str} to the power of {second_number_str} is {answer_int}.')
print(words_str)
这样你就会知道不要在 *_str
变量上调用 str()
也不要在 *_int()
变量上调用 int()
等等。你也可以考虑在计算器中使用 float
而不是 int
。
我正在学习 python,我正在尝试制作另一个计算器。当我尝试 运行 时,前几个命令有效,但当我到达第 6 行时,它说:
TypeError: can only concatenate str (not "int") to str
代码在这里:
if user_input==' squares':
first_number=input(str(inp_words))
second_number=input(str(sec_inp_words))
f_num=str(first_number)
s_num=str(second_number)
answer=int(first_number)**int(second_number)
print('the answer to '+str(f_num)+'to the power of'+str(s_num)+'is'+answer)
print(str(words))
sys.exit()
这是一种使用格式化字符串的方法:
print(f'the answer to {f_num} to the power of {s_num} is {answer}.')
当您在字符串前添加 f 或 F 时,该字符串称为格式化字符串,就像您在字符串末尾添加 .format() 一样。 使用格式化字符串,您无需担心类型转换。
在花括号中,您可以在其中放置任何有效的 python 表达式,例如函数,因为 f 字符串是在运行时求值的。@AnnZen 的回答应该可以解决您眼前的问题 (+1)。但是你似乎有一个更大的问题来处理 int
和 str
并记住哪个变量是哪个:
second_number=input(str(sec_inp_words))
s_num=str(second_number)
answer=int(first_number)**int(second_number)
print('the answer to '+str(f_num)+'to the power of'+str(s_num)+'is'+answer)
因为您多次将字符串转换为字符串!一个更好地处理这个问题的方法可能是在变量名称中包含变量类型:
inp_words_str = "Please enter the first number: "
sec_inp_words_str = "Please enter the second number: "
words_str = "Goodbye!"
# ...
if user_input_str == 'squares':
first_number_str = input(inp_words_str)
second_number_str = input(sec_inp_words_str)
first_number_int = int(first_number_str)
second_number_int = int(second_number_str)
answer_int = first_number_int ** second_number_int
print(f'The answer to {first_number_str} to the power of {second_number_str} is {answer_int}.')
print(words_str)
这样你就会知道不要在 *_str
变量上调用 str()
也不要在 *_int()
变量上调用 int()
等等。你也可以考虑在计算器中使用 float
而不是 int
。