I hope I'm not too annoying . I started ruby 2 weeks ago.
I'm trying to put my calculator in a loop so it restart after calculation ends. Something like "Would you like to try again?"
def add(a, b)
puts "ADDING #{a} + #{b}"
puts "The result is #{a + b}"
end
def arg1()
puts "You chose option 1"
print "please enter first entry "
first_number = gets.to_i
print "Please enter second entry "
second_number = gets.to_i
add(first_number,second_number)
end
def selection()
puts "please enter your option : "
puts "For Adding : 1 "
puts "For Subtacting : 2 "
print "> "
end
selection
options_of_choice = gets.to_i
if options_of_choice == 1
arg1()
elsif options_of_choice == 2
arg2()
else
puts " Restarting"
end
calculator_on = true
while calculator_on
selection
options_of_choice = gets.to_i
if options_of_choice == 1
arg1()
elsif options_of_choice == 2
arg2()
else
puts " Restarting"
end
puts "do you want to try again?"
calculator_on = gets.chomp.downcase == 'y'
end
Related
I created a calculator in ruby. I am wondering how to put this in a loop so I don't have to run it constantly. I am new to programming so please understand I am I just trying to learn. I would appreciate any help provided.
puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing"
puts "Press a and enter to enable my services"
enable = gets.chomp
if enable == "a"
puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division"
else
"Puts Im Waiting..."
end
which_operation = gets.chomp
if which_operation == "+"
puts "What is the first number you want to add"
adding_first_number = gets.chomp.to_i
puts "What is the second number you want to add to #{adding_first_number}"
adding_second_number = gets.chomp.to_i
puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}"
else
end
if which_operation == "-"
puts "What is the first number you want to subtract"
subtracting_first_number = gets.chomp.to_i
puts "What is the number you want to subtract from #{subtracting_first_number}"
subtracting_second_number = gets.chomp.to_i
puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}"
else
end
if which_operation == "*"
puts "What is the first number you want to multiple"
multiplying_first_number = gets.chomp.to_i
puts "What is the number you want to multiple #{multiplying_first_number} by"
multiplying_second_number = gets.chomp.to_i
puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}"
else
end
if which_operation == "/"
puts "What is the first number to your divison question?"
dividing_first_number = gets.chomp.to_i
puts "What is the divisor?"
dividing_second_number = gets.chomp.to_i
puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}"
else
end
For example:
until (which_operation = gets.chomp).empty?
if which_operation == "+"
...
end
if which_operation == "-"
...
end
if which_operation == "*"
...
end
if which_operation == "/"
...
end
end
This loop will work until you press Enter without entering any text before it.
P.S.: Better use case operator instead of multiple if.
P.P.S.: All your code, after loop addition and without case operator, will be:
puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing"
puts "Press a and enter to enable my services"
until gets.chomp == "a"
puts "I'm Waiting..."
end
puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division"
until (which_operation = gets.chomp).empty?
if which_operation == "+"
puts "What is the first number you want to add"
adding_first_number = gets.chomp.to_i
puts "What is the second number you want to add to #{adding_first_number}"
adding_second_number = gets.chomp.to_i
puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}"
elsif which_operation == "-"
puts "What is the first number you want to subtract"
subtracting_first_number = gets.chomp.to_i
puts "What is the number you want to subtract from #{subtracting_first_number}"
subtracting_second_number = gets.chomp.to_i
puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}"
elsif which_operation == "*"
puts "What is the first number you want to multiple"
multiplying_first_number = gets.chomp.to_i
puts "What is the number you want to multiple #{multiplying_first_number} by"
multiplying_second_number = gets.chomp.to_i
puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}"
elsif which_operation == "/"
puts "What is the first number to your divison question?"
dividing_first_number = gets.chomp.to_i
puts "What is the divisor?"
dividing_second_number = gets.chomp.to_i
puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}"
end
puts "\nLet's try again: "
end
I am new to Ruby and working with this hangman style word guessing game. I have 2 main issues. Here is what I am working with now:
class Word_game
def initialize(word)
#word = word.downcase
#display_word = "_ " * word.length
end
def guess_the_word(word_guess)
word_guess.downcase
#word.split("").each_with_index do |word_letter, index|
if word_guess == word_letter
#display_word[index] = word_guess
p #display_word
puts "You're getting somewhere! Keep trying!"
end
end
if !#word.include? (word_guess)
puts "Nope, guess again..."
end
def win?
if #word == #display_word
puts "Congratulations you won!!! You are the word master!!!"
true
else
false
end
end
def lose?
if #attempts == 0
puts "You lose!!"
true
end
end
puts "Welcome to the Word Guessing Game! Let's see if YOU have what it TAKES!!!"
puts "This is a 2 player game. "
puts "Player 1... please enter a word for Player 2 to guess!"
puts ">>"
game_word = gets.chomp
game = Word_game.new(game_word)
attempts = 0
guessed_letters = []
until #attempts == game_word.length
puts "Ok Player 2, Guess a letter! GO!!!"
letter_guess = gets.chomp
if guessed_letters.include? letter_guess
puts "You already guessed that letter! Enter a new one."
letter_guess = gets.chomp
end
guessed_letters << letter_guess
game.guess_the_word(letter_guess)
if game.win?
attempts += 1
else game.lose?
end
end
end
First, the word progress should look like this if the word is hello:
h _ e _ _ o
Instead of this, the spaces are not in the right places and looks like this (an actual outcome of running my code):
.
Ok Player 2, Guess a letter! GO!!!
h
"h _ _ _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
o
"h _ o _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
e
"he_ o _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
l
"hel o _ _ "
You're getting somewhere! Keep trying!
"hello _ _ "
When the user guesses the word, it does not put my "congrats" statement and end the game.
I am also stuck on my 'lose' method. I am not sure how to fix the method so that the game ends when the user runs out of attempts and prints the "lose" statement.
Thanks for your help!
I think you're making the output too complicated. I would track the word and the guesses in an array. Instead of a display_word variable, I'd make it a method, possibly "to_s"
By the way, Ruby convention is to use CamelCase class names.
class WordGame
def initialize(word)
#word = word.downcase.chars
#guesses = ["_"] * #word.size
end
def to_s
#guesses.join " "
end
This should fix your spacing problem. This will also simplify guesses.
Also, the checking to see if you've already used the letter should probably be handled by the WordGame class.
For your first problem, your #display_word starts as follows:
[0] = '_' # For h
[1] = ' '
[2] = '_' # For e
[3] = ' '
...
When you guess 'e', for instance, you do:
#display_word[index] = word_guess
Where index equals 1, the second character in "hello", so as you can see it doesn't write to the 'e' index in #display_word.
For your second problem, there are a number of ways to fix it. For instance, I would do something like using #attempts_remaining starting from a value of 10 or so, then using the existing code:
if !#word.include? (word_guess)
#attempts_remaining -= 1 # Count failure to guess
puts "Nope, guess again..."
end
Then:
def win?
# If you've guessed all the letters, there's no '_' left in the display word
if !#display_word.include? ('_')
puts "Congratulations you won!!! You are the word master!!!"
true
else
false
end
end
def lose?
if #attempts_remaining == 0
puts "You lose!!"
true
end
end
Finally, tweak the until loop termination condition:
until game.win? or game.lose?
The existing calls to win? and lose? can be deleted.
(WORD MISSING GAME)
puts " "
puts "Total Round"
puts " "
puts "=> [Round-One ,Round-two,Round -three Round-four]"
puts " "
puts "=> [TOTAL 5 Tries]"
puts " "
one=""
two=""
three=""
four=""
puts " "
puts " --ROUND One press-- => (1)"
one=gets.to_i
puts '==================='
puts "Question:=> ( K ? N G )"
puts ""
c=5
5.times do
string1 = 'i'
stringone ="I"
puts "Answer:=> Try NO:#{c}"
string2 = gets.chomp
if (string1==string2)
puts "Good Work correct spaling"
break
elsif (stringone == string2 )
puts "Good Work correct spaling"
break
else
puts "-Worng spaling-"
end
c -=1
end
puts " Round Over "
if c<1
puts " Tries Over Game End "
exit
end
puts '==================='
puts "--ROUND Two press-- => (2)"
two=gets.to_i
puts '==================='
puts "Question:=> (P L ? Y )"
5.times do
string1 = 'a'
stringone = "A"
puts "Answer:=> Try NO:#{c}"
string2 = gets.chomp
if (string1==string2)
puts "Good Work correct spaling"
break
elsif (stringone==string2)
puts "Good Work correct spaling"
break
else
puts "-Worng spaling-"
end
c -=1
if c<1
puts " Tries Over Game End "
exit
end
end
puts " Round Over "
puts '==================='
puts "--ROUND Three press-- => (3)"
three=gets.to_i
puts '==================='
puts "Question:=> ( S P ? T )"
5.times do
string1 = 'o'
stringone= 'O'
puts "Answer:=> Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
break
elsif (stringone == string2)
puts "_Good Work correct spaling_"
break
else
puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts " *Round Over* "
puts '==================='
puts "--ROUND Four press-- => (4)"
four=gets.to_i
puts '==================='
puts "Question:=> ( G ? M E )"
5.times do
string1 = 'a'
stringone = "A"
puts "Answer:=> Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
break
elsif (stringone == string2)
puts "_Good Work correct spaling_"
break
else
puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts "**Yahoo Congragualtion complete All Round**"
Having some trouble checking for multiple sets of values.
loop do
turns(player1,player2)
break puts "you win!" if #entries.values[0..2].all?{|move| move == "X"}
end
I wanted to use a multi-dimensional hash like this. Is my syntax wrong or is this just something you cannot do?
loop do
turns(player1,player2)
break puts "you win!" if #entries.values[[0..2][3..5]].all?{|move| move =="X"}
end
another problem is when values[0..2] are already equal to "x" the loop doesn't break until the next turn.
Here is the repl
https://repl.it/JrQe/57
Source code
#entries = {1=>" ", 2=>" ", 3=>" ", 4=>" ", 5=>" ", 6=>" ", 7=>" ", 8=>" ", 9=>" "}
puts "###########################################"
puts " tic-tac-toe"
puts "\t\t 1 | 2 | 3 "
puts "\t\t-----------"
puts "\t\t 4 | 5 | 6 "
puts "\t\t-----------"
puts "\t\t 7 | 8 | 9 "
puts" ###########################################"
class Players
attr_accessor :name, :piece
end
puts "Player one, what is your name?"
player1 = Players.new
player1.name = gets.chomp
player1.piece = "X"
puts "Hello #{player1.name}, you have been assigned #{player1.piece}."
puts "Player two, what is your name?"
player2 = Players.new
player2.name = gets.chomp
player2.piece = "O"
puts "Hello #{player2.name}, you have been assigned #{player2.piece}."
def grid
puts "\t\t #{#entries[1]} | #{#entries[2]} | #{#entries[3]} "
puts "\t\t-----------"
puts "\t\t #{#entries[4]} | #{#entries[5]} | #{#entries[6]} "
puts "\t\t-----------"
puts "\t\t #{#entries[7]} | #{#entries[8]} | #{#entries[9]} "
end
def choice(grid)
name = gets.chomp
name2 = name.to_i
#entries.each do |key,value|
if name2 == key
#entries[key]= "X"
end
end
end
def choice2(grid)
name = gets.chomp
name2 = name.to_i
#entries.each do |key,value|
if name2 == key
#entries[key]= "O"
end
end
end
def turns(player1,player2)
puts "#{player1.name} make your move"
player1 = choice(grid)
puts "#{player2.name} make your move"
player2 = choice2(grid)
end
def win
loop do
turns(player1,player2)
break puts "you win!" if #entries.values[0..2].all?{|move| move == "X"}
end
I think the specific question you're asking is answered like this:
rows = [[0..2],[3..5],[6..8]]
cols = [[0,3,6],[1,4,7],[2,5,8]]
diag = [[0,4,8],[2,4,6]]
(rows + cols + diag).any?{ |line| line.all?{ |cell| #entries[cell] == "X"} }
Also, you can't have two expressions, break and puts before a trailing if statement. Try this:
if ...
puts "You win!"
break
end
Hey the code I created only repeats 2 times.
After I type the second time "y" for the "continue_question"-method the code only stops.
def greeting
puts "Hello! Please type your name: "
name = gets.chomp.capitalize
puts "It is nice to meet you #{name}. I am a simple calculator application."
puts "I can add, subtract, multiply, and divide."
end
greeting
def calculator
puts "First number: "
#n1 = gets.chomp.to_i
puts "Secons number: "
#n2 = gets.chomp.to_i
def calculation
puts "Type 1 to add, 2 to subtract, 3 to multiply, or 4 to divide two numbers: "
operation_selection = gets.chomp.to_i
if operation_selection == 1
#result = #n1 + #n2
elsif operation_selection == 2
#result = #n1 - #n2
elsif operation_selection == 3
#result = #n1 * #n2
elsif operation_selection == 4
#result = #n1 / #n2
else
puts "Something went wrong!"
calculation
end
end
calculation
puts "Your Result is #{#result}"
end
calculator
def continue_question
puts "Do you want to continue? (y/n)"
continue = gets.chomp.to_s
if continue == "y"
calculator
elsif continue == "n"
puts "Bye!"
else
puts "What?"
continue_question
end
end
continue_question
Your code does not repeat 2 times, it repeats once.
The reason is because in your continue_question method, you do not tell it to repeat again:
def continue_question
puts "Do you want to continue? (y/n)"
continue = gets.chomp.to_s
if continue == "y"
calculator # <-- This causes it it repeat ONCE!
elsif continue == "n"
puts "Bye!"
else
puts "What?"
continue_question
end
end
A quick fix is to re-call the continue_question method below that line, to recursively repeat itself:
def continue_question
puts "Do you want to continue? (y/n)"
continue = gets.chomp.to_s
if continue == "y"
calculator
continue_question # <-- Add this to repeat indefinitely
elsif continue == "n"
puts "Bye!"
else
puts "What?"
continue_question
end
end
The problem is that continue_question only executes once, at the end of your code, but you need to loop until user exits (i.e types n).
So simply add a loop inside continue_question, for example:
def continue_question
continue = "y"
until continue == "n" do
puts "Do you want to continue? (y/n)"
continue = gets.chomp.to_s
if continue == "y"
calculator
elsif continue == "n"
puts "Bye!"
else
puts "What?"
end
end
end
Hey the code I created only repeats 2 times.
I think you misunderstand the following:
if continue == "y"
calculator
elsif continue == "n"
puts "Bye!"
When you call the above function calculator, you are still executing the continue_question function. So when the calculator finishes executing, continue_question will finish as well and the program will stop. For the wanted result you can try using a loop.
I am creating a quiz-maker program in Ruby. It opens and reads a text file, and is able to make a quiz of of the questions and answers in the text file. I am trying to let the user know how many question they got wrong after they complete the quiz.
The error I am getting is:
quiz.rb:180:in `+': can't convert Fixnum into String (TypeError)
from quiz.rb:180:in `<main>'
I get this error after it goes through all the questions.
Here is my code. correct_count is where I try to subtract 1 from the starting 10 everytime someone gets a question wrong. Let me know if you would like to see the text file.
questions = File.open("question.txt","r+")
array = {}
contents = questions.readlines
questions.close
contents.collect! do |x|
x.chomp
end
contents.collect! do |x|
x.split(',')
end
contents.each do |x|
array[x[0]] = x
end
correct_count = 10
question1 = contents[0][1]
choice11 = contents[0][2]
choice12 = contents[0][3]
answer11 = contents[0][4]
question2 = contents[1][1]
choice21 = contents[1][2]
choice22 = contents[1][3]
answer21 = contents[1][4]
question3 = contents[2][1]
choice31 = contents[2][2]
choice32 = contents[2][3]
answer31 = contents[2][4]
question4 = contents[3][1]
choice41 = contents[3][2]
choice42 = contents[3][3]
answer41 = contents[3][4]
question5 = contents[4][1]
choice51 = contents[4][2]
choice52 = contents[4][3]
answer51 = contents[4][4]
question6 = contents[5][1]
choice61 = contents[5][2]
choice62 = contents[5][3]
answer61 = contents[5][4]
question7 = contents[6][1]
choice71 = contents[6][2]
choice72 = contents[6][3]
answer71 = contents[6][4]
question8 = contents[7][1]
choice81 = contents[7][2]
choice82 = contents[7][3]
answer81 = contents[7][4]
question9 = contents[8][1]
choice91 = contents[8][2]
choice92 = contents[8][3]
answer91 = contents[8][4]
question10 = contents[9][1]
choice101 = contents[9][2]
choice102 = contents[9][3]
answer101 = contents[9][4]
topic = contents[11][1]
puts "Welcome to this " + topic + " quiz. Please spell the answers exactly right to get them correct (don't worry about caps). Good Luck!"
puts question1
puts choice11 + " or " + choice12 + "?"
user1 = gets.chomp
if user1.downcase == answer11.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question2
puts choice21.downcase + " or " + choice22 + "?"
user2 = gets.chomp
if user2.downcase == answer21.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question3
puts choice31.downcase + " or " + choice32.downcase + "?"
user3 = gets.chomp
if user3.downcase == answer31.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question4
puts choice41.downcase + " or " + choice42.downcase + "?"
user4 = gets.chomp
if user4.downcase == answer41.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question5
puts choice51.downcase + " or " + choice52.downcase + "?"
user5 = gets.chomp
if user5.downcase == answer51.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question6
puts choice61.downcase + " or " + choice62.downcase + "?"
user6 = gets.chomp
if user6.downcase == answer61.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question7
puts choice71.downcase + " or " + choice72.downcase + "?"
user7 = gets.chomp
if user7.downcase == answer71.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question8
puts choice81.downcase + " or " + choice82.downcase + "?"
user8 = gets.chomp
if user8.downcase == answer81.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
if user9.downcase == answer91.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "Next question: " + question10
puts choice101.downcase + " or " + choice102.downcase + "?"
user10 = gets.chomp
if user10.downcase == answer101.downcase
puts "correct"
else
puts "incorrect" and correct_count -1
end
puts "you got" + correct_count + "out of 10 correct"
Change:
puts "you got" + correct_count + "out of 10 correct"
to:
puts "you got" + correct_count.to_s + "out of 10 correct"
or:
puts "you got #{correct_count} out of 10 correct"
NOTE:
correct_count = 10
puts "incorrect" and correct_count -1
correct_count # => 10
# >> incorrect
correct_count still evaluates to 10.
You should be doing:
if user6.downcase == answer61.downcase
puts "correct"
else
puts "incorrect"
correct_count -= 1
end