raw_input python

时间:2015-05-30 13:26:44

标签: python

所以我是一个非常年轻的程序员,我使用python,并且我正在尝试在python中创建一个跟踪我的家务的应用程序,当你完成登录时,就像当你以我的身份登录时,当你完成时,它退出它,我希望我的父母能够看到那些东西,这就是为什么有登录,我希望它是变量空的地方,而你的父母登录,它说没有任何东西变量,并要求你把一些东西放在变量中,我也希望当我的个人资料结束时,它再次要求登录,这里是代码。(为了隐私,我已经从代码中取出了我的名字。)

print ("Welcome to Chore Tracker, ")

name = raw_input("Who is this?")

if name == "******": 
    c1 = raw_input("Have you kept your room clean?")
    c2 = raw_input("Did you fed IVAN?")
    c3 = raw_input("Have you fed your hamster?")
    c4 = raw_input("How many bags of trash have you taken out?")
    print "If you did all of those things, you have 6 dollars, plus " +     c4 + ", dollars!"
    print "If you did two of those things, you have 4 dollars, plus " + c4 + ", dollars!"
    print "If you did only one thing, you have two dollars, plus " + c4 + ", dollars!"

elif name == "Mom":
    print("Hello, Mom!, if an error pops up, that means I didn't put anything down yet!")
    print "Room clean, " + c1
    print "IVAN fed, " + c2
    print "Hamster fed, " + c3
    print "And I took out, " + c4 + ", bags of trash!"
    print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"

else:
    print("Hello, Dad!, if an error pops up, that means I didn't put anything down yet!")
    print "Room clean, " + c1
    print "IVAN fed, " + c2
    print "Hamster fed, " + c3
    print "And I took out, " + c4 + ", bags of trash!"
    print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"

2 个答案:

答案 0 :(得分:0)

if-elif-else通常被称为“分支”语句,因为在每种情况下,您都选择是否采用该分支执行。因此,如果name == "******",您只会执行第一个块而不是elifelse。但是,您只在第一个分支中声明c1c2c3c4,但在所有分支中使用它。因此,您需要将变量声明移到外部您的if-elif-else阻止,以便始终在if-elif-else阻止之前执行。

实施例

print ("Welcome to Chore Tracker, ")

name = raw_input("Who is this?")
c1 = raw_input("Have you kept your room clean?")
c2 = raw_input("Did you fed IVAN?")
c3 = raw_input("Have you fed your hamster?")
c4 = raw_input("How many bags of trash have you taken out?")

if name == "******": 
    print "If you did all of those things, you have 6 dollars, plus " +     c4 + ", dollars!"
    print "If you did two of those things, you have 4 dollars, plus " + c4 + ", dollars!"
    print "If you did only one thing, you have two dollars, plus " + c4 + ", dollars!"

elif name == "Mom":
    print("Hello, Mom!, if an error pops up, that means I didn't put anything down yet!")
    print "Room clean, " + c1
    print "IVAN fed, " + c2
    print "Hamster fed, " + c3
    print "And I took out, " + c4 + ", bags of trash!"
    print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"

else:
    print("Hello, Dad!, if an error pops up, that means I didn't put anything down yet!")
    print "Room clean, " + c1
    print "IVAN fed, " + c2
    print "Hamster fed, " + c3
    print "And I took out, " + c4 + ", bags of trash!"
    print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"

答案 1 :(得分:0)

目前最大的问题似乎是

  1. 您的变量在其中一个if分支中声明,因此无法从其他分支中访问
  2. 一旦你退出,(所以,你妈妈可以登录),你的变量就不会再填充了。
  3. 假设你保持程序运行,(这样2不是问题),这就是我的建议:

    c1 = "No"
    c2 = "No"
    c3 = "No"
    c4 = "0"
    
    def run():
        print ("Welcome to Chore Tracker, ")
        name = raw_input("Who is this?")
    
        if name == "******": 
            c1 = raw_input("Have you kept your room clean?")
            c2 = raw_input("Did you fed IVAN?")
            c3 = raw_input("Have you fed your hamster?")
            c4 = raw_input("How many bags of trash have you taken out?")
            print "If you did all of those things, you have 6 dollars, plus " +     c4 + ", dollars!"
            print "If you did two of those things, you have 4 dollars, plus " + c4 + ", dollars!"
            print "If you did only one thing, you have two dollars, plus " + c4 + ", dollars!"
    
        elif name == "Mom":
            print("Hello, Mom!, if an error pops up, that means I didn't put anything down yet!")
            print "Room clean, " + c1
            print "IVAN fed, " + c2
            print "Hamster fed, " + c3
            print "And I took out, " + c4 + ", bags of trash!"
            print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"
    
        else:
            print("Hello, Dad!, if an error pops up, that means I didn't put anything down yet!")
            print "Room clean, " + c1
            print "IVAN fed, " + c2
            print "Hamster fed, " + c3
            print "And I took out, " + c4 + ", bags of trash!"
            print "So I have 2, 4, or 6 dollars, plus, " + c4 + ", dollars!"
    
    
    while True:
        run()
    

    初始化初始化意味着您的程序不会抛出错误,并在您登录前显示当前状态。

    无限while循环将确保您的程序在用户完成后再次请求登录。