Coding a guessing game in Jython Environment for Students ( JES ) - if-statement

I am trying to code a guessing game in JES using the pseudo code below:
Generate a random single digit number
Have the user guess the number
If the user does not guess correctly give them a clue – indicate whether the number is even or odd and ask the user to guess again.
If the user does not guess correctly, give another clue – indicate whether the number is a multiple of 3 and ask the user to guess again.
If the user does not guess correctly, give another clue – indicate whether the number is less than or greater than 5 and ask the user to guess again.
If the user does not guess correctly,
display a box indicating the correct answer and the number of guesses the user made.
If the user has guessed correctly, display a box indicating their guess is correct and how many guesses were required
Show the amount of time the user played the guessing game.
There are so many conditions I have no clue how to have the program ask each question, analyze the answer, and if the condition isn't met, to move on to the next one. When I run this code, it is just stuck in the loop of asking the user to guess and does not move through the if statements to give the user hints if they are incorrect.
Below is my current code which obviously is incorrect. I am also aware there may be indenting issues in this post.
from random import *
from time import *
def main():
a= randrange( 0, 11 )
start= clock()
numberOfGuesses= 0
userGuess= requestInteger( " Please guess a single digit number " )
while userGuess != a:
userGuess= requestInteger( " Please guess a single digit number " )
if userGuess % 2 == 0:
showInformation( " Incorrect! Please try again. Hint: The number is even " )
if userGuess % 2 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is odd " )
if userGuess % 3 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is not a multiple of 3 " )
if userGuess > 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
if userGuess < 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
else:
showInformation( " Correct " )
REVISED AND WORKING CODE
from random import*
from time import*
def main ():
a= randrange( 0, 10 )
start= clock()
numberOfGuesses= 0
userGuess= requestNumber( " Please guess a single digit number " )
end= clock()
elapsedTime= end - start
if userGuess != a and userGuess % 2 == 0:
showInformation( " Incorrect! Please try again. Hint: The number is even " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
elif userGuess != a and a % 2 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is odd " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a % 3 != 0:
showInformation( " Incorrect! Please try again. Hint: The number IS NOT a multiple of 3 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a > 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a < 5:
showInformation( " Incorrect! Please try again. Hint: The number is less than 5 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess == a:
showInformation( " Correct! " )
showInformation( " The correct answer was" + " " + str(a) + " It took you " + str(elapsedTime) + " " + " seconds to complete this game " )
elif userGuess != a :
showInformation( " Incorrect! " + " " + " The correct answer was" + " " + str(a) + " It took you " + str(elapsedTime) + " " + " seconds to complete this game " )

its stuck in the loop because user cant guess the same num that randnum genertes
"while userguess !=a " it'll never be true

Related

C++ question: User think a number 1-100, program asks questions no more than 6 times to get answer. can't get correct [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
User has to think of a number from 1 - 100. Computer keeps asking not more than 6 times to get what
the user think number. I could not get the logic right, I don't know how to fix anymore. Please somebody help me.
#include "std_lib_facilities.h"
// this program is not correct
// try 24 you will see it
int main()
{
cout << "Think of a number between 1 to 100\n";
int max{100};
int min{0};
int answer{0};
string response{"??"};
while ((max - min) != 1)
{
cout << "Is the number < " << (max + min) / 2 << "?\n";
cin >> response;
if (response == "y" || response == "yes")
{
max = ((max + min) / 2);
if ((max - min) == 1)
answer = min;
}
else if (response == "n" || response == "no")
{
min = (max + min) / 2;
if ((max - min) == 1)
answer = max;
}
else
{
error("Invalid response\n");
}
}
cout << "The answer is " << answer << endl;
keep_window_open();
return 0;
}
Thank you in advance.
As has already been pointed out in the comments section, there are at least 3 bugs in your code:
Your question states that the user should think of a number between 1 and 100, but the variables min and max are initialized to 0 and 100, as if the user was supposed to think of a number between 0 and 100. Therefore, you should initialize min to 1 instead of 0.
When the user replies "yes" to the question whether the number is below a certain value, you set max to this value. This does not make sense, because you know that the number cannot be this value, but must be below this value. Therefore, you should set max to this value minus 1.
When min == 1 and max == 2, it would make sense for the next question your program asks to be whether the number is "< 2", in order to determine whether the number is 1 or 2. However, in that case, your program asks whether the number is "< 1", which does not make sense, because it already knows that the answer to that question is "no". Therefore, instead of asking whether the number is smaller than (max + min) / 2, it would make more sense to ask whether the number is smaller than (max + min + 1) / 2.
I have cleaned up your code a bit and fixed the bugs mentioned above. Here is the code:
#include <iostream>
#include <string>
constexpr int MIN = 1;
constexpr int MAX = 100;
int main()
{
int min{ MIN };
int max{ MAX };
std::string response;
std::cout << "Think of a number between " << MIN << " and " << MAX << "." << std::endl;
while ( min != max )
{
int guess = (max + min + 1) / 2;
std::cout << "Is the number < " << guess << "? ";
std::cin >> response;
if ( response == "y" || response == "yes" )
{
max = guess - 1;
}
else if ( response == "n" || response == "no" )
{
min = guess;
}
else
{
std::cout << "Invalid response" << std::endl;
}
//The following line only exists for debugging purposes and can be disabled
std::cout << "min: " << min << " max: " << max << std::endl;
}
std::cout << "The number is " << min << "." << std::endl;
return 0;
}
I have rearranged the code in such a way that the numbers 1 and 100 are hard-coded only in one place, instead of several places in the program. This allows you to change the range very easily, without having to change the numbers in several places in the program.
One thing that my code does not do is stop asking after 6 questions, because it can take up to 7 questions to find the correct answer. In your question, you specified that it should ask not more than 6 times, but did not specify what should happen if it has not found the answer by then.

Getting the second last value of a 4 digit integer

I'm trying a lab exercise which wants user to input a 2 4-digit integer. Then the program will extract all the numbers in the 4-digit integer and use the number to do an arithmetic calculation just like the image link below.
Arithmetic Calculation with 2 4-digit integer
However, the objective for this lab exercise is not to allow me myself, to use a for loop to obtain the result.
For instance, when i want to obtain the last number of the 4 digit integer, I could easily do it by using this.
int firstDigit = firstNo % 10; //i will get 4 if the integer is 1234
int secondDigit = secondNo % 10; //i will get 8 if the integer is 5678
And of course table formatting is nothing to worry about before getting the logic right. Next is a very simple calculation of the numbers using the digit i obtain from the above.
int addfirstNumbers = firstDigit + secondDigit;
int minusfirstNumbers = firstDigit - secondDigit;
int multiplefirstNumbers = firstDigit * secondDigit;
int modfirstNumbers = firstDigit % secondDigit;
double divfirstNumbers = firstDigit / secondDigit;
cout << "add: " << addfirstNumbers << endl
<< "minus " << minusfirstNumbers << endl
<< "multipile " << multiplefirstNumbers << endl
<< "remainder " << modfirstNumbers << endl
<< "division " << divfirstNumbers << endl;
I do understand forloop can make my life easier. But i'm just trying out the long method before trying out the shorter way which is forloop.
But even before i proceed, I'm still unable to extract out the other digit from this 4 digit integer.
Like Mike Vine mentioned in the comments, you can do integer division before taking the modulo.
#include <iostream>
int main(){
int x = 1234;
std::cout << (x/10)%10 << "\n";
}
#Output
3
Edit: This works for all places of a number. To find the nth value from the end, just keep adding 0s to the divisor. For example to find the 2nd from the last, you'd want to divide x by 100 instead.
You could simply do
int secondLastDigit = ((i - (i % 10)) % 100)) / 10;
For i=5678:
i % 10 (5678 % 10) equals 8
i - (i % 10) (5678 - 8) therefore equals 5670.
(i - (i % 10)) % 100 (5670 % 100) equals 70
Finally (i - (i % 10)) % 100) / 10 (70 / 10) = 7
This is pretty simple, just use the modulus operator on the number for 100(num%100), getting the last two digits that way, and then divide that result by ten, and store the digit in an int (so the decimal is properly truncated.)

Why do I get an Invalid Syntax from print in Python?

So I'm fairly new to programming and I'm just paying around trying to make some programs. This one is self-explanatory, but why do I get an invalid syntax for 'print' in line 12 (the first 'elif' statement)?
while True:
temp = raw_input("Would you like to convert:\nCelsius to Fahrenheit (press 1)\nor\nFahrenheit to Celsius (press 2)\n")
if temp == 1:
celsius = raw_input("What is the temperature in degrees Celsius?")
tempFahr = ((int(celsius)*(9/5))+32)
print "When it is " + celsius + " degrees celsius, it is " + tempFahr + "degrees fahrenheit."
elif temp == 2:
fahrenheit = raw_input("What is the temperature in degrees Fahrenheit?")
tempCel = ((int(fahrenheit)-32)*(5/9)
print "When it is " + fahrenheit + " degrees fahrenheit, it is " + tempCel + "degress celsius."
elif temp = 42:
print "You're a winner!"
else:
print "That is not a valid option."
print "Press 'enter' to input another value"
raw_input()
Also, if I over complicated something, I would really appreciate if you could point out what it was. Try not to correct me too much, though, I would like to try and figure it out on my own.
There are two syntax errors. First, you forgot a closing ) in the tempCel line, which confuses Python about the next print:
tempCel = ((int(fahrenheit)-32)*(5/9)
print "When it is " + fahrenheit + " degrees fahrenheit, it is " + tempCel + "degress celsius."
Then you used = where you meant ==:
elif temp = 42:
There are other errors -- for example, you're comparing temp, which is a string, to 1 and 2, which are integers, and you also might want to type 5/9 at the console to see what it gives you -- but they're not SyntaxErrors.
you should try
print("When it is " + str(fahrenheit) + " degrees fahrenheit, it is " + str(tempCel) + " degress celsius.")

lvalue required as left operand of assignment error via if statement

I've looked at similar questions, but being a total newbie at this, they haven't helped much, when I try to run my final if statement I run into this error, how can I make my cout statement clearer? Its intended purpose is to output how many bags of garbage it can accept, how many the user is trying to give it, and how many bags will be left over if it cannot take them all.
while(( reg < 50) && (met< 20) && (glass < 20))
{
reg=reg+reg; met=met+met; glass=glass+glass;
cout<< " I have enough "<< endl;
if(reg+=reg > 50){
cout<< "I can only accept " << 50 - (reg+=reg) << "of your " << (reg+=reg)<<" regular bags of garbage, I'll leave the other " << 50 - (reg+= reg)<< " I'll leave the other " << reg- (50 - reg+=reg)<< endl;
}
50 - reg += reg;
operator+= has lower precedence than operator-. The above statement is interpreted as:
(50 - reg) += reg;
which won't work. You probably wanted:
50 - (reg += reg);

probability c++ question

I have to make a game of craps and towards the end, I have to do some probability. Here is my code so far. I want it so that the loop repeats 1000 times and looks for the 'probNumb' that the user entered. I am not sure if did this right but lets say I entered the number 5. This is what I get.
"Out of 1000 times, 5 was rolled 1000 times."
So, its not counting how many times 5 was rolled. I am not allowed to use break or continue statements, only loops and if else.
cout &lt&lt "What number do you want the probability of ?";
cin >> probNumb;
while (probCount &lt 1000)
{
ranNumb= 1 + (rand() % (5 + 1));
ranNumb2= 1 + (rand() % (5 + 1));
ranNumbFin = ranNumb + ranNumb2;
probCount++;
if (ranNumbFin = probNumb)
probNumbCount++;
}
cout &lt&lt "Out of 1000 times, " &lt&lt probNumb &lt&lt " was rolled "
&lt&lt probNumbCount &lt&lt "times." &lt&lt endl;
if (ranNumbFin = probNumb) is either a typo or should use ==
It's 1000 because the assignment returns the value assigned and since that's always non-zero in this case, it's always true.
it's a typo
if (ranNumbFin = probNumb)
should be
if (ranNumbFin == probNumb)
Your line if (ranNumbFin = probNumb) should be if (ranNumbFin == probNumb) - you're assigning, not comparing, which is causing the probNumbCount to increment every time.
My use of C and C++ is rusty, but I believe the ranNumb and ranNumb2 are not going to behave like dice rolls. These will just give a uniform random variate over 0 to 1.
Conceptually, for a six sided dice:
u = rand();
if(u < 1/6) ranNumb=1;
elseif(u < 2/6) ranNumb=2;
elseif(u < 3/5) ranNumb = 3;
An so on. There is probably a better performing method.