2017-07-17 64 views
0

我是Python编程新手。我正在尝试编写我的代码,以便在输入年龄时打印一系列文本。但是,我的代码只能工作,如果我按照它逐行。例如,当我输入年龄2000+时,什么都不会发生。我需要先输入一个整数小于12,其次是整数比2000年Python - 基本编码

print('Please input name') 
if input() == 'Alice': 
    print('Hi, Alice.Please input age') 

if int(input()) < 12: 
    print('You are not Alice, kiddo.') 
elif int(input()) > 2000: 
    print('Unlike you, Alice is not an undead, immortal vampire.') 
elif int(input()) == 100: 
    print('You are not Alice, grannie.') 
elif 12 < int(input()) < 99: 
    print('You are Alice!.') 
+6

每次不要重复输入,将其分配给一个变量,比较变量 – PRMoureu

+0

如果你有你的答案,然后将其关闭。 – cSharma

回答

1

每次你去到另一个分支在你if你要求用户输入另一个时代!相反,做到以下几点:

age = int(input()) 
if age < 12: 
    print('You are not Alice, kiddo.') 
elif age > 2000: 
    print('Unlike you, Alice is not an undead, immortal vampire.') 
elif age == 100: 
    print('You are not Alice, grannie.') 
elif 12 < age < 99: 
    print('You are Alice!.') 
1
print('Please input name') 
if input() == 'Alice': 
    print('Hi, Alice.Please input age') 

age = int(input()) # take input and assign it on a variable 

if age < 12: 
    print('You are not Alice, kiddo.') 
elif age > 2000: 
    print('Unlike you, Alice is not an undead, immortal vampire.') 
elif age == 100: 
    print('You are not Alice, grannie.') 
elif 12 < age < 99: 
    print('You are Alice!.') 
1

input随后当()调用每次。所以在ifelif的多个input()是没有必要的。

商店的input()结果等age = int(input()),然后使用在ifelifage代替。

3

在这里我写了你的理解目的代码。采用新变量,以便不需要重复输入()方法几次。此外,Age验证代码保持在第一个条件内,当第一个条件成立时它将被执行。

print('Please input name') 
var = input() 
if var == 'Alice': 
    print('Hi, Alice.Please input age') 
    var = input() 
    try: 
     if int(var) < 12: 
      print('You are not Alice, kiddo.') 
     elif int(var) > 2000: 
      print('Unlike you, Alice is not an undead, immortal vampire.') 
     elif int(var) == 100: 
      print('You are not Alice, grannie.') 
     elif 12 < int(var) < 99: 
      print('You are Alice!.') 
    except Exception as ex: 
     print('Invalid Data: Error: ' + ex) 
else: 
    print ("Invalid Name") 
+2

我想我们可以假设OP使用基于'print'函数的Python 3。因此,很可能'raw_input'不可用,'input'实际上是正确的。 – SethMMorton

+0

哦,是的,上面的查询与P3相关,但是定义逻辑是正确的,所以只需使用input()方法而不是raw_input(),其余部分都是相同的。 – cSharma

+0

如果我是初学者,并且看到您的答案并决定将其复制/粘贴到我的代码中并运行它以更好地理解正在发生的事情,我将得到一个'SyntaxError',您可以在其中调用'print'而不带括号, 'raw_input'上的'NameError'。我会比以前看到这个答案更困惑。而不是像现在这样留下你的答案,只是说评论中需要修正的问题,你应该编辑答案,以便正确。 – SethMMorton

3
var = input('Please input name ') 
if var == 'Alice': 
    var = int(input('Hi, Alice.Please input age ')) 
    if var < 12: 
     print('You are not Alice, kiddo.') 
    elif var > 2000: 
     print('Unlike you, Alice is not an undead, immortal vampire.') 
    elif var == 100: 
     print('You are not Alice, grannie.') 
    elif 12 < var < 99: 
     print('You are Alice!.') 
else: 
    print ("Invalid Name") 

此代码的工作,因为它会询问一次,并尝试看看,如果一些条件是真实的,而不是每次都询问。

1

input()函数返回一个字符串。引用the docs(重点煤矿):

然后,该函数读取从输入的线,将其转换成字符串(剥离的后换行),和返回该

因此,在每个if当您拨打input()时,您必须输入一个新的字符串。因此,您必须先输入一个低于12的整数。

要解决此问题,您需要将原始输入存储在变量中。现在,正如文档所说,input()返回一个字符串。所以,要么你可以施放(使用int())在每种情况下的整数,这样做:

if int(age) < 12: 

和存储变量为一个字符串。

虽然,除非你没有任何特别的原因让年龄为字符串,我建议你同时存储变量中的年龄摆在首位的字符串转换:

age = int (input()) 

在这种情况下,age将有一个int。

0

我们希望,这是你在找什么:

while True: 
name = input("Please ENTER your name: ") 
if name == "Alice": 
    print("Hi Alice!") 
    break 
print("Sorry, your name isn't correct. Please re-enter.") 

age = False 
while age != True: 
     age = int(input("Please ENTER your age: ") 
     age = True 
     if age < 12: 
      print("You're not Alice, kiddo.") 
      age = False 
     elif age > 2000: 
      print("Unlike you, Alice is not an undead, immortal vampire.") 
      age = False 
     elif age == 100: 
      print("You're not Alice, Granny!") 
      age = False 
     else: 
      print("You are Alice!") 
      age = True