Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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/9/solr/3.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 如何根据用户提出的问题使用变量计算公式_Python_Python 2.7 - Fatal编程技术网

Python 如何根据用户提出的问题使用变量计算公式

Python 如何根据用户提出的问题使用变量计算公式,python,python-2.7,Python,Python 2.7,我在第5行得到错误“TypeError:can not multiply sequence by non int of type‘str’”,即“number=str(c(c*r)**x)”。如果有任何帮助,我都会感激的。我是个新手 import math c = raw_input("what is the intial number?") r = raw_input("What is the rate of growth?") x = raw_input("How many years ar

我在第5行得到错误“TypeError:can not multiply sequence by non int of type‘str’”,即“number=str(c(c*r)**x)”。如果有任何帮助,我都会感激的。我是个新手

import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
int(c)
int(r)
int(x)
number = str(c(c*r)**x)
print (number)

您忘记重新分配变量:

import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = str(c*(c*r)**x)
print (number)
int()
返回解析后的整数。您还需要为正在执行的每个乘法运算输入
*

print
默认情况下可以打印整数,无需在
number

import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = (c * (c * r))  ** x
print(number)

更好的方法只是在同一个语句中,像这样(用户友好的方式)

输出


c、 r,x是转换它的字符串,你必须执行
c=int(c)
你错过了
c(c*r)
位<代码>c不是可调用的好吧,祝你好运。
import math

c = int(raw_input("what is the intial number?\n=> "))
r = int(raw_input("What is the rate of growth?\n=> "))
x = int(raw_input("How many years are taking place?\n=> "))

number = str(c*(c*r)**x)
print(number)
what is the intial number?
=> 3
What is the rate of growth?
=> 4
How many years are taking place?
=> 5
746496