Simple Guessing Game in Python that doesn't work - python

I am trying to make a simple guessing game. When I run the code and print the number in advance to check if the program is working, it keeps on having the same wrong response. In other words, even if the guess variable is equal to the num variable, the program still returns "Incorrect!", and I can't figure out why. Thank you in advance. The code is pretty self-explanatory, so I'll post it here.
import random
num = random.randint(1, 6)
print(num)
guess = input(f'Guess a number: ')
if guess == num:
print(f'Correct!')
else:
print(f'Incorrect!')

You are comparing different types. num is an integer number while the guess is a string. You need to convert it to an integer before comparing them.
Try using num == int(guess) instead.

Related

python calling empty list invalid syntax

I am trying to create an empty list and for some reason it is telling me it's invalid syntax? it also flags the next line with the same error, saying that while count<amount: is invalid. am i wrong for thinking this doesnt make sense? using vsc. thanks in advance.
my code looks like this.
list=[]
count=0
while count < amount :
s=int(input"enter a number:")
list.append(s)
count= count+1
i tried to use list={}, list=() even though i know those are wrong. it also flags lines like list4=[1,3] ??
amount is not defined. Define it with a number like 5 and try then.
You also need to make sure the variable list is called something else, it is a python-reserved word.
Lastly, make sure the input function has parenthesis () around it. e.x. input("enter number: ")
In python the indent is 4 spaces.
You need to change the variable name "list" because it is a built in name in python.
You need to put brackets after input.
amount = 5
numberList = []
count = 0
while count < amount:
s = int(input("enter a number:"))
numberList.append(s)
count += 1
I think the problem there is that you're using input() wrong, it should be:
int(input("Enter a number: "))
Also, I am assuming that you defined amount earlier in your code, otherwise you will need to in order for your code to work :D
I also saw a comment saying that list is a python reserved function: It is, however you can use it as a variable name and it will not return an error :)
Have a good day :D

Program that prints whether the first input number is greater than the second input number

I’m trying to help my son with python and I’m not much help.
The task is - write a python program that asks the user to enter two numbers (number1 and number2) and then prints whether the first number is greater than the second number or prints the second number is bigger than the first number
I wrote:
Number1 = input(“please enter your first number”)
Number2 = input(“please enter your second number”)
Then I’ve crashed out ☹️
Number1 = int(input('please enter your first number'))
Number2 = int(input('please enter your second number'))
if Number1 > Number2:
print('Number1 bigger than Number2')
elif Number1 < Number2:
print('Number1 smaller than Number2')
else:
print('Number1 equal to Number2')
The Mo Huss's answer is the simplest and most effective solution to this problem. In addition, you may also want to put an exception catcher like this:
try:
Number1 = int(input('please enter your first number'))
Number2 = int(input('please enter your second number'))
except ValueError:
print('please provide numbers')
Basically, this prevents your application from crashing if you input a string, or nothing at all, since Python won't be able to convert that to a number (via the int() function) and therefore would raise a ValueError exception.
You may even complicate things a little by looping over the code until you input two valid numbers, but I think that goes beyond the scope of your original question.
Cheers!
PS: Also, you mentioned that your program just crashes. Does that mean it crashes by default (i.e., you try to run it but it crashes) or do you do something specific for it to crash?
As you've been told already, there seems to be nothing that would make the code crash. Maybe check that the double quotation marks (") are formatted properly; if you use certain text editors it will change from a default, vertical double comma to curly ones (for instance, Microsoft Word does this, but that's not a suggested code editor in the first place, for many reasons, including this one).

Loop function yet break if certain code put into raw_input

I've been looking up on here and Google yet can't find the solution for what I'm looking for. "While" function keeps failing me as I don't know here I need to put it. I'm a total newbie so if you could kindly explain the solution, thank you:
number = raw_input("Number: ")
if int(number) % 2 == 0:
print "Even number"
else:
print "Odd number"
All I would like to do is to keep the function looping allowing the user to enter as many numbers as possible. It should only break if user puts in "stop" as the value.
Well if user types stop, program stops.So we need to check for that when we create our while loop like:
number_from_user = raw_input("Number: ")
while number_from_user != "stop":
try:
if int(number_from_user)% 2 == 0:
print "Even number"
else:
print "Odd number"
number_from_user = raw_input("Number:")
except ValueError:
print("Enter a number please")
number_from_user = raw_input("Number:")
And I suggest you make yourself familiar with while loops, which I found a video explaining and making an example "Guess the number game" using python. It should help you enough to make you able to solve your own problem with while loops.
https://www.youtube.com/watch?v=tu0zlBFRa_c
or:
https://www.youtube.com/watch?v=PoPxclbw6uc
And I suppose you're using python 2.7, in those videos they're using python 3+, you should type raw_input where they're typing input.
Edited: Added try and except.

asking for user input repeatedly python

I'm trying to make a small program in Python that generates a random number (from the throw of a die) and asks the user to guess what the number is, and if the user guesses wrong, then depending on whether the guess is higher than the actual number or not the program is supposed to re-prompt them for another guess.
The problem that I'm having with the code is that the output is always "That's a little high. Try again!", even if I input all the possible values.
Can someone please help me figure out which portion of the code is generating the error?
import random
dice = random.randint(1,6)
answer = raw_input ("What do you think is the number?")
while (answer != dice):
if answer > dice:
print ("That's a little high. Try again!\n")
else:
print ("That's a little low. Try again!\n")
answer = raw_input ("What do you think is the number?")
print ("That's correct! Good job. \n")
Thank you!
raw_input returns a string, which is always bigger than a number from 1-6 as its numerical representation is taken in comparison. you need to cast the input to int
answer = int(raw_input ("What do you think is the number?"))

Beginner: While loop not functioning properly, syntax errors, displays nothing

I am working through some Looping exercises, While statements in particular. Here are the instructions:
2.2) Modify the program so that it asks users whether they want to guess again each time. Use two variables, number for the number and answer for the answer to the question whether they want to continue guessing. The program stops if the user guesses the correct number or answers "no". (In other words, the program continues as long as a user has not answered "no" and has not guessed the correct number.)
Here is my code:
#!usr/bin/env python
#
#While statement
number = 24
while number != 24:
answer = raw_input("Guess my lucky number! Do you want to keep guessing?")
if number == 24:
print "You got it! That is great!"
elif answer == "no":
print "Thank you for playing."
else:
print "That is not the right answer! Try again."
When I run the module in IDLE, the end quote of That is great!" - becomes red and says invalid syntax. In terminal if I run $ python while.py nothing loads. I've tried writing this as Python 3 functions with print("") but it still does not run.
Thanks to anyone who can help with this.
The while-cycle is never entered because the condition number != 24 never holds.
This is why there is no output.
Your loop never executes because you state that number = 24, and then right after, your while loop will only start if number != 24.
In addition, raw_input will yield a string, not an int so either ask for "24" or cast the raw_input to an int.
It also seems that you don't actually give the user a chance to guess the number at all; you only ask the user if s/he wants to keep playing.
You might want to do something like this:
number = 24
answer = ""
while answer != str(number):
answer = raw_input("Guess my lucky number, or type 'no' to quit.")
if answer == "no":
print "Okay, see you later"
break
elif answer != str(number):
print "wrong number"
if answer == str(number):
print "you got it right"
Here's the syntax issues:
answer = ""
while answer != "24":
answer = raw_input("Guess my lucky number! Do you want to keep guessing?")
if answer == "24":
# You can fill in the rest ...
Well, I don't want to straight out solve it for you, but take a look at your conditional in the while loop. Think about what happens, line-by-line, when you run it, particularly in the "number" variable.
The other answers are touching on the problem but there are more...
Yes you really should be checking the answer variable in your loop instead of the number variable, but also keep in mind that raw_input is going to give you a string. So you will not get an int 24, you will get a string "24". The idea here is to take the answer variable from raw_input and check that variable against both your number and the value "no"
number = "24"

Categories