找到3个数字的乘积和总和

时间:2017-09-07 17:13:22

标签: python

在python程序中编写一个程序来查找总和或产品,当你按s时它会找到总和,当你按p找到product.if你输入其他字符时它会打印你输入的错误字符.ch值不与s或p.error s未定义

a = input("enter a number\n")
b = input("enter a number\n")
c = input("enter a number\n")
print "enter s for sum and p for product"
ch = input("enter character")
if(ch=='s'):
    s = a+b+c
    print "the product of the number:" +s
elif(ch =='p'):
    p = a*b*c
    print "the product of the number:" +p
else:
    print "entered invalid character"

此代码给出

Name error: name 's' is not defined

1 个答案:

答案 0 :(得分:0)

使用raw_input从输入读取字符串。

a = input("enter a number\n")
b = input("enter a number\n")
c = input("enter a number\n")
print "enter s for sum and p for product"
ch = raw_input("enter character")
if(ch=='s'):
    s = a+b+c
    print "the product of the number:" +str(s)
elif(ch =='p'):
    p = a*b*c
    print "the product of the number:" +str(p)
else:
    print "entered invalid character"