2013-03-28 98 views
-2

我只是在练习几行代码,这不是针对任何特定的程序,但是搞砸我的是我希望输入光标出现在打印旁边,但是在同一时间我需要在同一行中定义一个变量,因为e不能等于4,所以我将e定义为无效。这里是我的代码:if-then语句与比较两个变量

a = int(input("Give me a Number: ")) 
b = int(input("Give me another number: ")) 
c = b + a 
def none(): 
    pass 
e = none() 
print("the sum of"), a, ("and"), b, ("is"), c 

d = int(input("Please Insert your age here: ")) 
if d < 18: 
    print("Sorry you must be 18 years or older to enter this site") 
else: 
    print("Welcome to www.example.com") 
print("What is 2+2: "), e == int(input("")), 
if e == 4: 
    print("good Job!") 
else: 
    print("sorry no") 
+1

而不是'e = none()'你可以做'e = None'。 '=='是'做这两件事平等吗?'运算符,而不是赋值运算符,它是一个'='。 – Patashu 2013-03-28 06:03:51

+0

这里有一个有效的一般问题(来自OP缺乏Python经验),但我似乎无法以任何明智的方式概括原文。投票结果太局部。 – brice 2013-03-29 21:15:28

回答

3

你的意思是这样的:

print("What is 2+2: "); e=int(input("")) 

你当前的代码实际上是试图创建的printNone)结果的tuple和表达式的结果e==int(input("")) - 如果e由于尚未定义NameError而没有给出NameError,通常这会是一个布尔值。


需要注意的是,通常在这种情况下,你可能只是做:

e = int(input("What is 2+2: ")) 
1

注意,有(e == int(input(""))之间的差异(这意味着e等于表达)和e = int(input(""))这意味着将表达式值赋值为e

您需要指定值到e