简体   繁体   English

output 与 output 不同。 我究竟做错了什么?

[英]The output is different from the output. What am I doing wrong?

I'm new to python programming.我是 python 编程的新手。 This is my assignment for my first final sem.这是我第一次期末考试的作业。 If the user enter their facility as "1" then user as "Yes".如果用户将其设施输入为“1”,则用户输入为“是”。 The system should show the price accordingly.系统应相应地显示价格。 This is my code but, when my input is "2" and "yes" or any number with "yes", it's always showing the output for facility == 4.I don't know what I'm doing wrong here.这是我的代码,但是,当我的输入是“2”和“yes”或任何带有“yes”的数字时,它总是显示 output for facility == 4。我不知道我在这里做错了什么。 It's really hard to find the mistake in this huge codes.在这个巨大的代码中很难找到错误。 Is there anyways to shorten this.反正有没有缩短这个。 If yes, do help me out.如果是,请帮帮我。

if facility == 1 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 100
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 100
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 100
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 100
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 100
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 150
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 150

if facility == 2 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 100
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 100
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 100
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 100
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 100
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 150
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 150

if facility == 3 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 200
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 200
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 200
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 200
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 200
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 250
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 250

if facility == 4 and user == "Yes" or user == "YES" or user == "yes":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 30
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 30
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 30
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 30
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 30
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 60
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 60

if facility == 1 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 150
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 150
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 150
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 150
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 150
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 200
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 200

if facility == 2 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 150
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 150
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 150
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 150
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 150
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 200
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 200

if facility == 3 and user == "No" or user == "NO" or user == "no":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 250
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 250
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 250
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 250
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 250
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 300
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 300

if facility == 4 and user == "No" or user == "no" or user == "NO":
    if day == "Monday" or day == "MONDAY" or day == "monday":
        price = 35
    elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
        price = 35
    elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
        price = 35
    elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
        price = 35
    elif day == "Friday" or day == "FRIDAY" or day == "friday":
        price = 35
    elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
        price = 65
    elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
        price = 65

The problem is that and takes precedence over or .问题在于and优先于or An expression like:像这样的表达式:

facility == 4 and user == "No" or user == "no" or user == "NO"

is the same as:是相同的:

(facility == 4 and user == "No") or (user == "no") or (user == "NO"):

To have this expression do what you want it to do, you'd want to use something more like one of the following:要让这个表达式做你想做的事,你会想使用更像以下之一的东西:

facility == 4 and (user == "No" or user == "no" or user == "NO")
facility == 4 and user in ("No", "no", "NO")
facility == 4 and user.lower() == "no"

Here is how I would write this logic more compactly -- use Enums to standardize all the string values (this protects you from typos and makes it easy to validate that the input is one of the expected values), and then put all the logic into a dict instead of a bunch of if/elif .以下是我将如何更紧凑地编写此逻辑的方法——使用 Enums 标准化所有字符串值(这可以保护您免受拼写错误,并且可以轻松验证输入是预期值之一),然后将所有逻辑放入一个 dict 而不是一堆if/elif The dict can be made more compact by the fact that you don't have individual prices for each day, simply different weekday/weekend prices:由于您没有每天的单独价格,只是不同的工作日/周末价格,因此可以使字典更加紧凑:

from enum import Enum

class User(Enum):
    YES = "yes"
    NO = "no"

class Day(Enum):
    MONDAY = "monday"
    TUESDAY = "tuesday"
    WEDNESDAY = "wednesday"
    THURSDAY = "thursday"
    FRIDAY = "friday"
    SATURDAY = "saturday"
    SUNDAY = "sunday"
    def is_weekend(self) -> bool:
        return self in (Day.SATURDAY, Day.SUNDAY)


# Prices are keyed on facility number(int), User, and Day.is_weekend().
prices = {
    1: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
    2: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
    3: {User.YES: {False: 200, True: 250}, User.NO: {False: 250, True: 300}},
    4: {User.YES: {False: 30, True: 60}, User.NO: {False: 35, True: 65}},
}

price = prices[facility][User(user.lower())][Day(day.lower()).is_weekend()]

Try using if/elif instead of if/if.尝试使用 if/elif 而不是 if/if。 Also, put brackets around the if logic, like so:另外,在 if 逻辑周围加上括号,如下所示:

if facility == 4 and (user == "Yes" or user == "YES" or user == "yes":)

The problem is that your if statement logic is not correct.问题是您的if语句逻辑不正确。 When you do the following check:当您执行以下检查时:

if facility == 4 and user == "Yes" or user == "YES" or user == "yes":

This is functionally equivalent to:这在功能上等同于:

if (facility == 4 and user == "Yes") or (user == "YES") or (user == "yes"):

Meaning, if the user chose a variation of "Yes" , then the if statement will always be entered.意思是,如果用户选择了"Yes"的变体,那么将始终输入if语句。 Your logic should look like this instead:您的逻辑应如下所示:

if facility == 4 and (user == "Yes" or user == "YES" or user == "yes"):

Which could be simplified to this, by just comparing the lowercase version of the user string:通过比较用户字符串的小写版本,可以将其简化为:

if facility == 4 and user.lower() == "yes":

You could simplify all your logic to use lowercase, which would simplify your if statements to look like this:您可以简化所有逻辑以使用小写字母,这将简化您的if语句,如下所示:

if facility == 4 and user.lower() == "yes":
    if day.lower() == "monday":
        price = 30
    elif day.lower() ==  "tuesday":
        price = 30
    elif day.lower() == "wednesday":
        price = 30
    elif day.lower() == "thursday":
        price = 30
    elif day.lower() == "friday":
        price = 30
    elif day.lower() == "saturday":
        price = 60
    elif day.lower() == "sunday":
        price = 60

To shorten this code you can use .lower() which turns a String into lowercase.要缩短此代码,您可以使用.lower()将字符串转换为小写。

user = user.lower()
day = day.lower()

This will allow you to just check whether day == "monday" instead of looking through different capitalizations of Monday.这将允许您只检查是否day == "monday"而不是查看 Monday 的不同大小写。

You could also do你也可以做

if day in ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]

This will remove most of your elif statements and just put them all in one.这将删除您的大部分elif语句,并将它们全部放在一个中。

暂无
暂无

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

相关问题 Python function 在调用时不返回结果,没有 output。 我究竟做错了什么 - Python function does not return reuslt when called upon, no output. What am I doing wrong 我究竟做错了什么? -输出到python 3.x中的文件 - What am I doing wrong? - Output to file in python 3.x 我做错了什么? Output 值低于一个量 - What I am doing wrong? Output values below an amount 一些输入的 output 应该是“错误的数字”,但我没有得到那个输入。 我究竟做错了什么? - The output for some of the inputs should be ‘Wrong number’ but I am not getting that input. What am I doing wrong? 我得到错误的python输出格式。 我究竟做错了什么? - Im getting wrong python output format. what am i doing wrong? 我的代码没有返回所需的输出。 我哪里错了? - My code is not returning the desired output. Where am I going wrong? 过滤Python列表时出现意外输出:我在做什么错? - Unexpected output when filtering Python list: What am I doing wrong? 损坏/损坏 output.mp4 完全无法播放...我做错了什么 - broken/corrupted output .mp4 completely unplayable... what am i doing wrong 我在 output 的末尾得到 { }。 是什么原因造成的,我该如何删除它? - I am getting { } on the end of my output. What is causing this and how do I remove it? 有什么我做错了吗? 我没有收到任何错误,但没有显示 output - Is there something I am doing wrong? I get no errors but no output is displayed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM