为什么这个Python程序中的菜单不起作用?

时间:2016-03-11 17:11:34

标签: python-2.7 menu

我已经100次查看了这段代码,我觉得它可能是我想念的小东西。程序将允许您登录,并显示菜单,但在输入您的选择后,它会再次连续显示菜单。

 import sys

def main():
    login = 'yes'
    choice = 0
    hours = [0] * 7
    wages = 0
    totalHours = 0
    totalWages = 0
    print
    login = raw_input('Do you want to login?')
    while not (login == 'yes' or login == 'no'):
        print
        print 'Please enter a yes or no'
    while login == 'yes':
        print
        userId = raw_input ('Enter user name:')
        passWord = raw_input('Enter password:')
        while passWord != userId:
            print 'Incorrect Password, please try again.'
            passWord = raw_input('Enter password:')
        while passWord == userId:
            print 'Login Success!'
            print
            print 'Enter 1 to view upcoming schedule'
            print 'Enter 2 to view previous schedule'
            print 'Enter 3 to calculate wages due'
            print 'Enter 4 to verify clock in/out times'
            print
            choice = raw_input('Enter 1 to 4 from menu.')

def readFromNext(nextWeek):
    inFile = open('nextweek.txt', 'r')
    str1 = inFile.read()
    print str1
    str2 = inFile.read()
    print str2
    print
    inFile.close()

def readFromLast(lastWeek):
    inFile = open('lastweek.txt', 'r')
    str1 = inFile.read()
    print str1
    str2 = inFile.read()
    print str2
    print
    inFile.close()

def getHours(hours):
    counter = 0
    while counter < 7:
        hours[counter] = input('Enter hours worked per day')
        counter = countr + 1
    return hours

def getTotalHours(hours, totalHours):
    counter = 0
    while counter < 7:
        totalHours = totalHours + hours[counter]
        counter = counter + 1
    return totalHours

def getWages(wages):
    wages = input('Enter your hourly wage.')
    return wages

def getTotalWages(totalHours, wages):
    totalWages = totalHours * wages
    print 'Your total pay due is:' , totalWages
    return totalWages

def readFromClock(clockHours):
    inFile = open('clockhours.txt', 'r')
    str1 = inFile.read()
    print str1
    str2 = inFile.read()
    print str2
    print
    inFile.close()

    while choice != '5':

        if choice == '1':
            readFromNext(nextWeek)
            print 'Upcoming schedules'


        if choice == '2':
            readFromLast(lastWeek)
            print 'Previous schedules'


        if choice == '3':
            hours = getHours(hours)
            totalHours = getTotalHours(hours, totalHours)
            wages = getWages(wages)
            totalWages = gettotalWages(totalHours, wages)
            print 'Paycheck calculator'

        if choice == '4':
            readFromClock(clockHours)
            print 'Clock in/out times'


main()

2 个答案:

答案 0 :(得分:0)

您未在login循环中更改while not的值。

login = 'default'
while not (login == 'yes' or login == 'no'):
    print 'Please enter a yes or no'
    login = raw_input('Do you want to login?')

......例如。你现在拥有它的方式永远不会结束:

while not (login == 'yes' or login == 'no'):
    print
    print 'Please enter a yes or no'

这在概念上与:

相同
while True:
    if login == 'yes' or login == 'no':
        break
    print
    print 'Please enter a yes or no'

因此,如果登录永远不会改变,它将永远不会结束。

答案 1 :(得分:0)

您期望它做什么?您添加了许多功能,但它们似乎未使用。您最终会反复打印菜单,因为您处于循环中

while passWord == userId:
    ...

如果你想要其他任何事情发生,你必须对choice变量做一些事情并突破该循环。

例如:

choice = raw_input('Enter 1 to 4 from menu.')
if choice == 1:
    readFromNext(nextWeek)
elif choice == 2:
    readFromLast(lastWeek)
elif choice == 3:
    getTotalWages(totalHours, wages)
elif choice == 4:
    getHours(hours)

显然你需要找出哪个函数映射到哪个用户输入,但是目前你没有做任何用户选择的事情,只是要求他们一遍又一遍地选择而不能控制循环的终止。< / p>