繁体   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')

您的 if else 语句缩进是错误的。 通常四个空格用于缩进,比制表符更受欢迎。 在行继续中可以忽略缩进。 但总是缩进是个好主意。

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')

你的if AnswerToHowAreYouToday: a应该是这样if AnswerToHowAreYouToday == a @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 

您可以看到最后打印了两个 Good 为了避免这些问题,您可以像这样改进代码

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.')

然后输出将是:

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