我的while循环在这里出了什么问题?循环一次

时间:2015-04-10 04:36:00

标签: python-2.7 while-loop

我试图让它循环直到用户输入有效数字

def daysoftheweek():
    print("1-Sunday")
    print("2-Monday")
    print("3-Tuesday")
    print("4-Wednesday")
    print("5-Thursday")
    print("6-Friday")
    print("7-Saturday")
daysoftheweek()
def whatistoday():
    day = input("Please enter the number day it is:")
    return day
day2 = whatistoday()
def todayis(day2):
    day2 = whatistoday()
    while True:
        if day2 < 1 or day2 > 7:
            print ("Please enter a valid input")
            return day2
        elif day2 == 1:
            print("So today is Sunday")
            break
        elif day2 == 2:
            print("So today is Monday")
            break
        elif day2 == 3:
            print("So today is Tuesday")
            break
        elif day2 == 4:
            print("So today is Wednesday")
            break
        elif day2 == 5:
            print("So today is Thursday")
            break
        elif day2 == 6:
            print("So today is Friday")
            break
        elif day2 == 7:
            print("So today is Saturday")
            break
todayis(day2)

2 个答案:

答案 0 :(得分:0)

试试这个:

def daysoftheweek():
    print("1-Sunday")
    print("2-Monday")
    print("3-Tuesday")
    print("4-Wednesday")
    print("5-Thursday")
    print("6-Friday")
    print("7-Saturday")
daysoftheweek()
def whatistoday():
    day = input("Please enter the number day it is:")
    return day
day2 = whatistoday()
def todayis(day2):
    while True:
        if day2 == "1":
            print("So today is Sunday")
            break
        elif day2 == "2":
            print("So today is Monday")
            break
        elif day2 == "3":
            print("So today is Tuesday")
            break
        elif day2 == 4:
            print("So today is Wednesday")
            break
        elif day2 == "5":
            print("So today is Thursday")
            break
        elif day2 == "6":
            print("So today is Friday")
            break
        elif day2 == "7":
            print("So today is Saturday")
            break
        else:
            day2 = whatistoday()
todayis(day2)

答案 1 :(得分:0)

你可以试试这个。这是根据你的代码:

def daysoftheweek():
    print("1-Sunday")
    print("2-Monday")
    print("3-Tuesday")
    print("4-Wednesday")
    print("5-Thursday")
    print("6-Friday")
    print("7-Saturday")
daysoftheweek()
def whatistoday():
    day = input("Please enter the number day it is:")
    return day
day2 = whatistoday()
def todayis(day2):
    while True:
        if day2 < 1 or day2 > 7:
            print ("Please enter a valid input")
            day2 = whatistoday()
        elif day2 == 1:
            print("So today is Sunday")
            break
        elif day2 == 2:
            print("So today is Monday")
            break
        elif day2 == 3:
            print("So today is Tuesday")
            break
        elif day2 == 4:
            print("So today is Wednesday")
            break
        elif day2 == 5:
            print("So today is Thursday")
            break
        elif day2 == 6:
            print("So today is Friday")
            break
        elif day2 == 7:
            print("So today is Saturday")
            break
todayis(day2)