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

时间:2019-12-24 07:40:50

标签: 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')

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

您的var entery = Enteries() func AddToDatabase () { entery.dates.append(dateString) print(entery.dates) } 应该像这样的if AnswerToHowAreYouToday: a。 @Santhos提到了它,但是当您的代码运行输出时,显示如下

if AnswerToHowAreYouToday == a

您最后可以看到两个好。为了避免这些问题,您可以像这样改善代码

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