麻烦在python中的if语句

时间:2013-10-15 18:16:25

标签: python if-statement while-loop

x=0
y=0
while x !="exit" or y !="exit":
    x= raw_input("Enter your name: ")
    y= raw_input("Enter your grade: ")
    if y!= "exit":
        g=int(y)
    if g<50 or g>100:
        print("Invalid input")
    else:
        if g>=50 and g<70:
             print("not a good grade,work harder")
        if g>=70 and g<90:
            print("Nice grade, try to get higher next time")
        if g>=90 and g<=100:
            print("Excellent grade!")
print("Good bye")

所以基本上这是我的程序,它工作正常,但我遇到了一些问题 首先,当我在x和y中键入exit时,程序才会退出,一旦在x或y上键入exit,我需要退出。
另一个问题是,一旦程序关闭,(一旦我在x和y中键入退出),结果是:

Enter your name: exit
Enter your grade: exit
Nice grade, try to get higher next time
Good bye

如果只键入一个退出,如何退出程序? 以及如何解决打印的问题

Nice grade, try to get higher next time  
Good bye 

如果只键入一个出口,如何使循环退出?
如何修复结果,一旦键入退出,它将只打印“再见”?

4 个答案:

答案 0 :(得分:2)

break检查if后,您需要y != "exit"声明。如果y == "exit"

,我已将检查替换为中断

此外,您可以使用if-elif-else块,并简化最后一个if条件,因为它现在是冗余的。此外,您可以简化条件本身以使用a < b < c格式以提高可读性。

x=0
y=0
while x !="exit" or y !="exit":
    x= raw_input("Enter your name: ")
    if x == "exit":
        break
    y= raw_input("Enter your grade: ")
    if y == "exit":
        break
    g=int(y)
    if 50 <= g <= 100:
        if 50 <= g < 70:
             print("not a good grade,work harder")
        elif 70 <= g < 90:
            print("Nice grade, try to get higher next time")
        else:
            print("Excellent grade!")
    else:
        print("Invalid input")
print("Good bye")   

答案 1 :(得分:1)

这应该是如何做到这一点的一个很好的例子:

#grade test
while True:
    #set name
    name=raw_input("Enter your name: ")
    #test if name is quit
    if str(name)=='quit':
        break
    #set grade
    grade=raw_input("Enter your grade: ")
    #test if grade is quit
    if str(grade)=='quit':
        break
    #if it isn't quit, set grade to a number
    grade=int(grade)
    #evaluate the actual grade
    if grade<0 or grade>100:
        print("Invalid input")
    elif grade>=50 and grade<70:
        print("not a good grade,work harder")
    elif grade>=70 and grade<90:
        print("Nice grade, try to get higher next time")
    elif grade>=90 and grade<=100:
        print("Excellent grade!")
    else:
        print("Consider getting a tutor..")
#since we broke when we entered quit, we have exited the loop and now we can quit the program
print "Goodbye!"
exit(1)

让我知道它是怎么回事!

答案 2 :(得分:1)

while x !="exit" or y !="exit":
除非x和y ='退出'

,否则

将返回true

您所描述的行为需要

while x !="exit" and y !="exit":

以允许x或y结束while循环。

您要求的其他行为(在输入'exit'后立即中断)可以通过在if子句中包含其余循环来测试'exit'来轻松完成

您正在描述的其他不稳定行为(打印语句)就像是由缩进错误引起的。

这是我认为你正在描述的版本,使用在Python 3.3中运行的原始代码

x=0
y=0
while x !="exit" and y !="exit":
    x= input("Enter your name: ")
    if x != 'exit':
         y= input("Enter your grade: ")
         if y!= "exit":
              g=int(y)
              if g<50 or g>100:
                   print("Invalid input")
              else:
                   if g>=50 and g<70:
                        print("not a good grade,work harder")
                   elif g>=70 and g<90:
                        print("Nice grade, try to get higher next time")
                   else:
                        print("Excellent grade!")
print("Good bye")

答案 3 :(得分:0)

这只是一些非常基本的逻辑,但是替换

x= raw_input("Enter your name: ")
y= raw_input("Enter your grade: ")

x= raw_input("Enter your name: ")
if x == "exit": break
y= raw_input("Enter your grade: ")
if y == "exit": break