在控制台中运行 python 简单代码时遇到问题 [英] Having trouble with python simple code running in console
本文介绍了在控制台中运行 python 简单代码时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我运行了这段代码.然后它在控制台中显示了以下内容.
I ran this code.Then it displayed the follwing in the console.
回溯(最近一次调用最后一次):文件",第 13 行,在 NameError 中:名称 'r' 未定义
Traceback (most recent call last): File "", line 13, in NameError: name 'r' is not defined
import math
p = int(raw_input("Please enter deposit amount: \n"))
i = int(raw_input("Please input interest rate: \n"))
t = int(raw_input("Please insert number of years of the invesment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p(1+r*t)
B = p(1+r)^t
if interest == "simple":
print int(float(A))
elif interest == "compound":
print int(float(B))
推荐答案
有多个错误和缩进问题;我评论他们哪里出错了:
There were multiple errors and indentation problem ;I commented them where you made mistakes:
p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")) #rename i to r
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p*(1+r*t) #multiply p with ()
B = p*(1+r)**t #same as B
#** is power
if interest == "simple":
print (float(A)) # you dont need to cast the float again to int
else: #since there is no other conditions it's obvious to print compound
print(float(B))# you dont need to cast the float again to int
这篇关于在控制台中运行 python 简单代码时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文