Python 如何根据用户提出的问题使用变量计算公式
我在第5行得到错误“TypeError:can not multiply sequence by non int of type‘str’”,即“number=str(c(c*r)**x)”。如果有任何帮助,我都会感激的。我是个新手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
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