while循环中的迭代器执行的时间比预期多一个

时间:2017-11-12 21:14:38

标签: ruby while-loop iterator

我正在尝试创建一个代码,直到一个人连续三次说'BYE'为止。

使用以下代码:

ask = gets.chomp
count = 0
while (count != 3)
  if (ask == 'BYE')
    puts 'HUH?!  SPEAK UP, SONNY!'
    count = count + 1
    ask = gets.chomp
  elsif (ask != ask.upcase)
    puts 'HUH?!  SPEAK UP, SONNY!'
    count = 0
    ask = gets.chomp
  else
    puts 'NO, NOT SINCE ' + rand(1930..1950).to_s + '!'
    count = 0
    ask = gets.chomp
  end
end
puts 'Goodbye for now'

我实际上需要输入'BYE'四次。有人能指出如何解决它吗?

3 个答案:

答案 0 :(得分:1)

您实际上不必四次输入BYE。只有三个就足够了。第四个输入可以是任何东西。

BYE # input 1
HUH?!  SPEAK UP, SONNY!
BYE # input 2
HUH?!  SPEAK UP, SONNY!
BYE # input 3
HUH?!  SPEAK UP, SONNY!
nah, I give up # input 4
Goodbye for now

您的代码存在的问题是您在选择处理程序后立即请求下一个输入。即使按照你的逻辑,你也不需要那些输入。所以你必须重做这一部分。

答案 1 :(得分:1)

在循环开始时只放置class CloudServerCreateSerializer(ModelSerializer): ... ext_one = serializers.CharField() ext_two = serializers.CharField() class Meta: model = CloudServer fields = "__all__" 一次修复它:

gets

执行:

count = 0

while count != 3
    ask = gets.chomp

    if ask == 'BYE'
        puts 'HUH?!  SPEAK UP, SONNY!'
        count = count + 1
    elsif ask != ask.upcase
        puts 'HUH?!  SPEAK UP, SONNY!'
        count = 0
    else
        puts 'NO, NOT SINCE ' + rand(1930..1950).to_s + '!'
        count = 0
    end
end

puts 'Goodbye for now'

答案 2 :(得分:0)

你的while (count != 3)是如此Javaish(不需要Ruby中的括号)我无法写出更多的rubyesque解决方案。没有更短,但更多DRY,你可能会在Ruby程序中看到更多的代码,当然也不是典范,在Ruby中总有很多方法可以做同样的事情。

class Strange
    def initialize(wanted)
        @answer = true
        @count  = 0
        @wanted = wanted # desired number of correct consecutive answers
    end

        # Increment @count if true, else reset to zero.
    def answer(boolean)
        @answer = boolean

        if boolean
        then # then is optional, but I like it
            @count = @count + 1
        else
            @count = 0
        end
    end

        # Write a message.
    def message(number)
        puts case number
        when 1 then 'HUH?!  SPEAK UP, SONNY!'
        when 2 then "NO, NOT SINCE #{rand(1930..1950)} !"
        else 'WHAT ?'
        end
    end

    def prompt
        print @answer ? 'Talk please > ' : 'Wrong answer, retry > '
        @ask = gets.chomp
    end

        # Recursively loop until the number of correct consecutive answers
        # corresponds to the desired number.
    def run
        prompt

        case
        when @ask == 'BYE'
            message 1
            answer(true)
        when @ask != @ask.upcase
            message 1
            answer(false)
        else
            message 2
            answer(false)
        end

        run unless @count == @wanted # recursive loop
    end
end # class Strange

Strange.new(3).run

puts 'Goodbye for now'

执行:

$ ruby -w t.rb 
Talk please > xyz
HUH?!  SPEAK UP, SONNY!
Wrong answer, retry > XYZ
NO, NOT SINCE 1935 !
Wrong answer, retry > BYE
HUH?!  SPEAK UP, SONNY!
Talk please > what ?
HUH?!  SPEAK UP, SONNY!
Wrong answer, retry > BYE
HUH?!  SPEAK UP, SONNY!
Talk please > BYE
HUH?!  SPEAK UP, SONNY!
Talk please > BYE
HUH?!  SPEAK UP, SONNY!
Goodbye for now