if / elsif / else Ruby错误

时间:2016-08-14 21:01:05

标签: ruby-on-rails ruby

有人能告诉我为什么我想出的Ruby on Rails解决方案是返回IF语句的输出而不管控制台中写的是什么? (如果你用“西班牙语”回答问题,你仍然得到一个“Bonjour!”作为回报......

作业2:全球问候

询问用户他们将如何做 喜欢受到欢迎。

如果他们说“用法语”,请回复 与“卓悦!”

如果他们说“用西班牙语”,请回复 与“Hola!”

如果他们说“在南非荷兰语”,请回复 与“你好!”

用“呃,嗨?”处理所有其他输入

    for blocks in cave_block1S_pos:
        screen.blit(cave_block,blocks)
        if move_playerR==True:
            cave_block1S_pos[0]-=5

    for blocks in cave_block2S_pos:
        screen.blit(cave_block,blocks)
    for blocks in cave_block3S_pos:
        screen.blit(cave_block,blocks)

#cave.append([random.randint(player_imgS_pos[0]-512,player_imgS_pos[1]+684), random.randint(player_imgS_pos[0]-512,player_imgS_pos[0]+384)])
    #for rocks in cave:
        #screen.blit(cave_block, rocks)
        #if move_playerR==True:
            #rocks[0]+=5

    if backgrounS_pos[0]>=32:
        backgrounS_pos[0]=32

    if backgrounS_pos[0]<=-905:
        backgrounS_pos[0]=-905

    if backgrounS_pos[1]>=32:
        backgrounS_pos[1]=32

    if backgrounS_pos[1]<=-470:
        backgrounS_pos[1]=-470

    if move_playerR==True:
        backgrounS_pos[0]-=5

    if move_playerL==True:
        backgrounS_pos[0]+=5

    if move_playerD==True:
        backgrounS_pos[1]-=5

    if move_playerU==True:
        backgrounS_pos[1]+=5

    print(backgrounS_pos)

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key==K_ESCAPE:
                pygame.quit()
                exit(0)

            if event.key==K_w:
                move_playerU=True

            if event.key==K_a:
                move_playerL=True

            if event.key==K_s:
                move_playerD=True

            if event.key==K_d:
                move_playerR=True

        if event.type==pygame.KEYUP:
            if event.key==K_w:
                move_playerU=False

            if event.key==K_a:
                move_playerL=False

            if event.key==K_s:
                move_playerD=False

            if event.key==K_d:
                move_playerR=False

        if event.type==pygame.QUIT:
            pygame.quit
            exit(0)
    pygame.display.update()

2 个答案:

答案 0 :(得分:5)

使用=进行作业。

您需要==,例如:

puts "How would you like to be greated?"
greet = gets.chomp

if greet == "in French"
    puts "Bonjour!"
elsif greet == "in Spanish"
    puts "Hola!"
elsif greet == "in Afrikaans"
    puts "Hallo!"
else
    puts "Uh, hi?"
end

您还可以使用case声明:

puts "How would you like to be greated?"
greet = gets.chomp

case greet 
when "in French"
    puts "Bonjour!"
when "in Spanish"
    puts "Hola!"
when "in Afrikaans"
    puts "Hallo!"
else
    puts "Uh, hi?"
end

如果您将它与正则表达式结合使用,您还可以捕捉不同的拼写或句子:

puts "How would you like to be greated?"
greet = gets.chomp

case greet 
when /French/i
    puts "Bonjour!"
when /Spanish/i
    puts "Hola!"
when /Afrikaans/i
    puts "Hallo!"
else
    puts "Uh, hi?"
end

答案 1 :(得分:0)

=是一个赋值运算符,==用于比较

if "in French" == "in French"  // returns true
    puts "Bonjour"
else
    puts "Uh, hi?"

value = 10 // assign 10 to the variable value
puts value //print 10