Python计算器代码不起作用

时间:2014-04-05 09:19:00

标签: python raspberry-pi

我最近有一个覆盆子pi,我正在学习如何使用它来编写python代码。我也有一个覆盆子用户指南,我复制了计算器脚本,但它没有用,你能帮忙吗? 这是我的剧本;

username = raw_input("What is your name?    ")
print ("Welcome to the program,",username
goAgain == 1
while goAgain == 1:
num1 = int(raw_input("type the first number"))   
num2 = int(raw_input("type the second number"))   
print num1, "added to", num2, "equals", num1 + num2   
print num1, "minus", num2, "equals", num1 - num2   
print num1, "multiplied by", num2, "equals", num1 * num2   
goAgain = int(raw_input("Type 1 to enter more numbers, or any other number to quit: ")) 

2 个答案:

答案 0 :(得分:1)

username = raw_input("What is your name?: ")
print ("Welcome to the program, ", username)

goAgain = 1 # <<< Equality check instead of assignment.

while goAgain == 1:
    num1 = int(raw_input("type the first number: "))
    num2 = int(raw_input("type the second number: "))

    print num1, "added to", num2, "equals", num1 + num2
    print num1, "minus", num2, "equals", num1 - num2
    print num1, "multiplied by", num2, "equals", num1 * num2

    goAgain = int(raw_input(
        "Type 1 to enter more numbers, "
        "or any other number to quit: "
    ))

enter image description here

PS:https://stackoverflow.com/editing-help - &gt;使用空格来正确缩进代码块

答案 1 :(得分:0)

您的代码具有左括号和等号(==)而不是赋值符号(=)。您的计划中还有一个IndentionError。固定代码是这样的:

username = raw_input("What is your name?    ")
print ("Welcome to the program,",username)
goAgain = 1
while goAgain == 1:
    num1 = int(raw_input("type the first number"))   
    num2 = int(raw_input("type the second number"))   
    print num1, "added to", num2, "equals", num1 + num2   
    print num1, "minus", num2, "equals", num1 - num2   
    print num1, "multiplied by", num2, "equals", num1 * num2   
    goAgain = int(raw_input("Type 1 to enter more numbers, or any other number to quit: "))

这应该有效