缩进错误python 1

时间:2015-04-22 16:59:37

标签: python indentation

我不知道为什么会有缩进区域,但它真的让我感到紧张

print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)
    if raw_input() = "yes"
        print "what is your password?"
    if raw_input() = "no"
        print "Suit yourself, %s!" % (name)

3 个答案:

答案 0 :(得分:1)

您的程序存在很多问题,因此我强烈建议您阅读一些教程

print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
    print "what is your password?"
if yes_no.lower() == "no":
    print "Suit yourself, %s!" % (name)

始终将raw_inputs分配给变量。

例: 更改此print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"

到此:

name = raw_input("first I\'d like to know a bit about you! What is your name? (No data is stored!!)"

其次

当您使用ifforwhile等时,您必须使用:结束语句以及您希望执行的任何语句你的条件应该是缩进的。

例如:

if yes_no == 'yes':
    print 'Ok'
print 'Ending program'

这样,程序的布局对用户来说将更加清晰。查看一些教程并试用它们

答案 1 :(得分:1)

首先,当您编写if语句时,缩进功能已关闭。您需要将它们带回一个标签空间。

以下是工作代码的示例:

print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"

print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)

print "What is your date of birth?"
dob = raw_input()

print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)

# I fixed the indentation below:

if raw_input() == "yes": #<-- fixed comparison operator here and added colon
    print "what is your password?"
if raw_input() == "no": #<-- fixed comparison operator here and added colon
    print "Suit yourself, %s!" % (name)

此处我还将=符号更改为===符号是您在声明变量时使用的标记,例如name = raw_input(),它使用name的值创建变量raw_input()

==符号是您在比较两个内容时使用的内容,例如raw_input() == "yes",用于检查raw_input()值是否等于"yes"。< / p>

答案 2 :(得分:0)

感谢 Josh B。 letsc ,我现在已经完成了代码:

print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
    print "what is your password?"
    passw = raw_input()
    if passw == "password":
       print "really? You\'ve got to be kidding me!"
    if passw == name:
        print "Anybody who knows your name will be able to guess that!"
    if passw == dob:
        print "Anyone who knows your birthday can guess that!"
    if len(passw) < 5:
        print "That password may not be long enough, %s" % (name)
    if len(passw) > 20:
        print "Are you sure you can remeber that?"
    else:
        print "That\'s a superb password, %s!!" % (name)
if yes_no.lower() == "no":
    print "Suit yourself, %s!" % (name)