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

时间:2019-04-18 14:29:53

标签: python python-2.7

第5行出现错误“ TypeError:无法将序列乘以'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)

3 个答案:

答案 0 :(得分:1)

您忘了重新分配变量:

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)

答案 1 :(得分:0)

int()返回已解析的整数。您还需要为要执行的每个乘法操作放置*

print默认情况下可以打印整数,不需要str左右的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)

答案 2 :(得分:0)

仅在同一条语句中做的更好的方法,就像这样(用户友好的方法)。

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