Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不';t变化变量_Python_Variables_Typeerror_Python 3.8 - Fatal编程技术网

Python 不';t变化变量

Python 不';t变化变量,python,variables,typeerror,python-3.8,Python,Variables,Typeerror,Python 3.8,我正在学习python,我正在尝试制作另一个计算器。当我尝试运行它时,前几个命令起作用,但当我到达第6行时,它会说: TypeError:只能将str(而不是“int”)连接到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(

我正在学习python,我正在尝试制作另一个计算器。当我尝试运行它时,前几个命令起作用,但当我到达第6行时,它会说:
TypeError:只能将str(而不是“int”)连接到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)

< P>这样,您就可以在<代码> ** STR 变量,不调用“代码> int())/>代码> > <代码> */代码>变量等。您也可以考虑在代码中使用<代码>浮点< /代码>而不是<代码> int >代码> .< /p>它应该是代码>…+ '是'+STR(回答))+1以获取解决问题的适当代码。但是要考虑更多的解释,而不是“应该这样做”。例如,一句关于最近的f字串功能的话可能适合解释发生了什么。