有if,else语句python的问题

时间:2017-06-06 05:57:41

标签: python

我正在学习python,正在为自己做一个练习,我遇到了一些问题。

  1. 如果我输入"是"这将返回无效的答案,因为它正在寻找"是"?

  2. 此外,即使我输入"是"我觉得我的if语句出错了。我正确地得到了#34;答案无效"打印。

  3. 关于如何改进的任何想法?

    attribute = {
        "G": "Greatest",
        "O": "Of",
        "A": "All",
        "T": "Time"
        }
    
    answer = str(raw_input("Will You Make Me Food?:"))
    
    for answer in range(2): 
        if answer == "Yes":
            print attribute 
        elif answer == "No":
            print "Your the Worst" 
        else:
            print "Invalid Answer"
    

2 个答案:

答案 0 :(得分:1)

处理用户输入时。 dani@saeedi:~/Desktop/website/myweb$ rails server /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2.rb:31:in `require': libmysqlclient.so.20: cannot open shared object file: No such file or directory - /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/mysql2.so (LoadError) from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2.rb:31:in `<top (required)>' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:82:in `require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:82:in `block (2 levels) in require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:77:in `each' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:77:in `block in require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:66:in `each' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:66:in `require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.15.1/lib/bundler.rb:108:in `require' from /home/dani/Desktop/website/myweb/config/application.rb:7:in `<top (required)>' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:88:in `require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:88:in `block in server' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `tap' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `server' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>' from /home/dani/Desktop/website/myweb/bin/rails:9:in `require' from /home/dani/Desktop/website/myweb/bin/rails:9:in `<top (required)>' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `load' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `call' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/client/command.rb:7:in `call' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/client.rb:30:in `run' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/bin/spring:49:in `<top (required)>' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `load' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `<top (required)>' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require' from /home/dani/.rbenv/versions/2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require' from /home/dani/Desktop/website/myweb/bin/spring:15:in `<top (required)>' from bin/rails:3:in `load' from bin/rails:3:in `<main>' 额外的空白区域总是最好的。并且在进行字符串比较时总是将事物变为大写或大写。所以从

开始
strip

然后

answer = raw_input("Will You Make Me Food?:").strip().lower()

完成后,检查你的for循环。您正在执行上述代码两次。 if answer == "yes": print attribute elif answer == "no": print "Your the Worst" else: print "Invalid Answer" 但您只收集一次输入。 yoyu或许意味着

for answer in range(2):

附录: 关于按顺序打印for answer in range(2): answer = raw_input("Will You Make Me Food?:").strip().lower() if answer == "yes": print attribute elif answer == "no": print "Your the Worst" else: print "Invalid Answer" 的问题。这是一个字典,因此不会按照您定义的顺序保存。为此,您可以使用元组

attribute

答案 1 :(得分:-3)

您的代码会进行区分大小写的搜索。您可以修改它,如下所示:

for answer in range(2): 
    if answer == "Yes" or answer == "yes":
        print attribute 
    elif answer == "No" or answer == "no":
        print "Your the Worst" 
    else:
        print "Invalid Answer"

或者您可以将输入读取到较低或较高的位置,并在您将其转换为相同的情况下进行搜索。

answer = str(raw_input("Will You Make Me Food?:")).lower()   # or .upper()
for answer in range(2): 
    if answer == "yes":
        print attribute 
    elif answer == "no":
        print "Your the Worst" 
    else:
        print "Invalid Answer"