简体   繁体   English

Python作业帮助请求

[英]Python homework help request

I need a little input on why I'm not getting the desired result from my code. 我需要一些输入,为什么我不能从代码中得到理想的结果。

Here's the code: 这是代码:

def main():
    SEAT_LIMIT_A=300
    print("Enter the total amount of tickets sold for Section A")
    sectionA=getTickets(0, SEAT_LIMIT_A)
    print("The total amount of tickets sold for section A is",sectionA)

def getTickets(ticket, SEAT_LIMIT):
    ticket=int(input("Enter:"))
    ticketsInvalid(ticket, SEAT_LIMIT)
    return ticket

def ticketsInvalid(ticket, SEAT_LIMIT):
    while ticket <0 or ticket > SEAT_LIMIT:
        print ("Error: The amount of tickets sold can't be less than 0 or more than",SEAT_LIMIT)
        ticket=int(input("Please enter the correct amount here: "))
        return ticket

main()

I basically need to get the number of tickets sold for a section in a movie theater, and then I need a validation loop to verify if the number entered is not less than or more than the seat limit for the section (in this case it's A, but I also need to implement more into the program once I figure out this section). 我基本上需要获取电影院某个区域的售票数量,然后需要一个验证循环来验证输入的数量是否不少于或超过该区域的座位限制(在这种情况下为A ,但是一旦弄清了这一部分,我还需要在程序中实现更多功能。 My professor wants us to create general functions to help complete this process for any section that needs to be checked. 我的教授希望我们创建一般功能,以帮助完成需要检查的任何部分的此过程。 She wants the main function to ask what was sold by calling the getTickets function, and then she wants the input from the getTicket function to be passed on to the ticketsInvalid function, so it can be checked, then she wants it to be passed back to the main function to be displayed. 她希望主函数通过调用getTickets函数来询问卖了什么,然后她希望将来自getTicket函数的输入传递给ticketsInvalid函数,以便可以对其进行检查,然后希望将其传递回要显示的主要功能。

Here's an output of a sample test of my code: 这是我的代码的示例测试的输出:

Enter the total amount of tickets sold for Section A
Enter:400
Error: The amount of tickets sold can't be less than 0 or more than 300
Please enter the correct amount here: 200
The total amount of tickets sold for section A is 400

I can't seem to figure out how I can get the amount entered at the validation loop to display in the main function correctly. 我似乎无法弄清楚如何使在验证循环中输入的金额正确显示在主函数中。 Any help without terribly altering my code would be very much appreciated. 任何不彻底改变我的代码的帮助将不胜感激。 I could easily complete this program with having to pass arguments from function to function, but that's what she wants... Sorry for the long post, there's just a lot of bull rules that she wants us to implement. 我可以很容易地完成该程序,而不必在函数之间传递参数,但这就是她想要的。很抱歉,很长一段时间,她只是想让我们实现许多繁琐的规则。 I am a complete newbie to programming, so please keep that in mind when you write your solutions/explanations :) 我是编程的新手,因此在编写解决方案/说明时请记住这一点:)

From the method getTickets(ticket, SEAT_LIMIT) , you are returning old value of ticket. 从方法getTickets(ticket, SEAT_LIMIT) ,您将返回票证的旧值。 The new value returned from ticketsInvalid(ticket, SEAT_LIMIT) is unused. ticketsInvalid(ticket, SEAT_LIMIT)返回的新值未使用。

So make a small modification in getTickets() method. 因此,请对getTickets()方法进行少量修改。

def getTickets(ticket, SEAT_LIMIT):
    ticket=int(input("Enter:"))
    return ticketsInvalid(ticket, SEAT_LIMIT)

when you assign new value to ticket variable in ticketsInvalid. 当您为ticketsInvalid中的票证变量分配新值时。 It create new int object and assign to ticket variable. 它创建新的int对象并分配给票证变量。 and you are not using return value of fun ticketsInvalid in getTickets. 并且您没有在getTickets中使用fun ticketsInvalid的返回值。

Hope you got it. 希望你明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM