Skip to content

Instantly share code, notes, and snippets.

@CodePint
Created December 11, 2017 20:16
Show Gist options
  • Save CodePint/14a4ba3feb5c493663cecbb695274984 to your computer and use it in GitHub Desktop.
Save CodePint/14a4ba3feb5c493663cecbb695274984 to your computer and use it in GitHub Desktop.
So in this program, allow the user to input the amount of sides on a dice and how many times it should be rolled.
From there, your program should simulate dice rolls and keep track of how many times each number comes up (this does not have to be displayed).
After that, print out how many times each number came up.
Subgoal
Adjust your program so that if the user does not type in a number when they need to,
the program will keep prompting them to type in a real number until they do so.
Put the program into a loop so that the user can continue to simulate dice rolls without having to restart the entire program.
In addition to printing out how many times each side appeared, also print out the percentage it appeared. If you can,
round the percentage to 4 digits total OR two decimal places.
=end
def ask_rolls
puts "how many dice do you want to roll?"
roll = gets.chomp.to_i
return
end
def ask_sides
puts "how many sides are on the dice"
sides = gets.chomp.to_i
return
end
def roll_dice
x = 1
y = ask_sides
results_array = Array.new
ask_rolls.times do rand_gen = rand(x..y)
results_array.push(rand_gen)
end
return results_array
end
def ask_loop
loop do
puts "lets roll some dice..."
puts "\n"
roll_dice
puts "you rolled a #{ask_sides} sided dice, #{ask_rolls} times"
put "here are the results #{roll_dice}"
puts "would you like to roll the dice again?"
puts "any key to continue, (n) to end the program"
roll_again = gets.chomp
if roll_again != "n"
puts "okay lets roll the dice again..."
ask_loop
else
puts "Thanks for rolling the dice..."
break
end
end
end
ask_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment