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

2024-05-28 20:51:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: nameyouhelloinputyourtodayarehow
2条回答

你的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 

最后你可以看到两张好的。为了避免这些问题,您可以这样改进代码

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. 

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

相关问题 更多 >

    热门问题