From: "David A. Black" Date: 2010-09-07T09:37:14+09:00 Subject: Re: Dice Roller ---993115281-1578034475-1283819819=:7670 Content-Type: MULTIPART/MIXED; BOUNDARY="-993115281-1578034475-1283819819=:7670" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---993115281-1578034475-1283819819=:7670 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Tue, 7 Sep 2010, Phlip wrote: > On Sep 6, 5:01�pm, Hd Pwnz0r wrote: >> It won't work. It's odd. It won't stop and it won't add a number onto x >> so it would say >> "Roll 1: 5 >> Roll 2: 3" >> etc >> >> It just repeats "Roll 1: (random number)". >> >> Also, is there a way for me to add one to the rand function so it won't >> get a zero? >> >> Attachments:http://www.ruby-forum.com/attachment/5009/dice.rb > > Next time just put it here: > > puts "How many dice do you want to roll?" > roll = gets.chomp > num_rolls = roll > y = 0 > x = y.to_i + 1 > while x <= roll.to_i > puts "Roll " + x.to_s + ": " + rand(max=6).to_s > end > > Take out the gets.chomp: > > roll = '12' # gets.chomp > > that lets you work on the loop without entering over and over again. That changes the behavior of the program pretty significantly, though. > then either fix your while loop by incrementing: x += 1 > > or use an each loop (which is truly the Ruby way): > > (0...roll.to_i).each do |x| > puts 'etc' > end I'd be inclined to use #times: print "How many dice do you want to roll? " num_rolls = gets.to_i num_rolls.times do |n| puts "Roll #{n+1}: #{rand(6) + 1}" end David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com ---993115281-1578034475-1283819819=:7670-- ---993115281-1578034475-1283819819=:7670--