简体   繁体   English

我的 Python if else 语句有什么问题?

[英]What is wrong with my my Python if else statement?

PersonsName = input('Enter your name: ')
print('Hello', PersonsName)

AnswerToHowAreYouToday = input('How are you feeling today? Are you doing Good or Bad?')


print('Ok', PersonsName,'So today you are basically feeling', AnswerToHowAreYouToday, '.')


a = "Good"
b = "Bad"
if AnswerToHowAreYouToday: a
print('Good')
else:
    print('Bad')

Your if else statement indentation is wrong.您的 if else 语句缩进是错误的。 Generally four whitespaces are used for indentation and is preferred over tabs.通常四个空格用于缩进,比制表符更受欢迎。 Indentation can be ignored in line continuation.在行继续中可以忽略缩进。 But it's a good idea to always indent.但总是缩进是个好主意。

PersonsName = input('Enter your name: ')
print('Hello', PersonsName)

AnswerToHowAreYouToday = input('How are you feeling today? Are you doing Good or Bad?')


print('Ok', PersonsName,'So today you are basically feeling', AnswerToHowAreYouToday, '.')


a = "Good"
b = "Bad"
if AnswerToHowAreYouToday == a:
    print('Good')
else:
    print('Bad')

Your if AnswerToHowAreYouToday: a should be like this if AnswerToHowAreYouToday == a .你的if AnswerToHowAreYouToday: a应该是这样if AnswerToHowAreYouToday == a @Santhos have mentioned it but when your code running your output display like this @Santhos 已经提到它但是当你的代码运行你的输出显示时

Enter your name: Deepak                                                                                                              
Hello Deepak                                                                                                                         
How are you feeling today? Are you doing Good or Bad?Good                                                                            
Ok Deepak So today you are basically feeling Good .                                                                                  
Good 

You can see two Good printed at last.您可以看到最后打印了两个 Good To avoid these issues you can improved your code like this为了避免这些问题,您可以像这样改进代码

PersonsName = input('Enter your name: ')
print('Hello', PersonsName)

AnswerToHowAreYouToday = input('How are you feeling today? Are you doing Good or Bad? ')

print('Ok', PersonsName,'So today you are basically feeling ', end = '')

a = "Good"
b = "Bad"
if AnswerToHowAreYouToday == a:
    print('Good.')
else:
    print('Bad.')

Then Output will be:然后输出将是:

Enter your name: Deepak                                                                                                              
Hello Deepak                                                                                                                         
How are you feeling today? Are you doing Good or Bad? Good                                                                            
Ok Deepak So today you are basically feeling Good. 

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

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