really basic newbie python [duplicate] - python

This question already has answers here:
How does my input not equal the answer?
(2 answers)
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
any reason why this doesn't work?
import random
answer = random.randint(2, 4)
print(answer)
txt = input("what number")
if answer == txt:
print("correct")
every time I enter the correct answer, it just comes back empty (or as an else statement if I put it)
I had this working last night although now I cannot work out why it won't, PS. i've just started to learn python this week so please go easy on me

you can not compare string with integer.
import random
answer = random.randint(2, 4)
print(answer)
txt = int(input("what number"))
if answer == txt:
print("correct")

Related

is there a way to get a if statement that says if a number is between 1 and 5, print something [duplicate]

This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Closed last month.
if money == (1, 799999):
print("You're a natural!)
I need to make it so, if the money variable is = to anything in between 1 and 799999 it can return a print. But i cant find anything to do it
You can use the following code:
if money>=1 and money<=799999:
print("You are natural.")
Hope it works.....

How to check a given number is palindrome or not? [duplicate]

This question already has answers here:
Python Palindrome
(3 answers)
Closed 2 months ago.
I have tried reversing a given string and then checking it but want to know the actual method to solve such questions without using any built-in functions of python.
Tried using built-in functions in python but expecting to know the code without using them.
z=int(input("Enter number:"))
temp=z
rev=0
while(z>0):
dig=z%10
rev=rev*10+dig
z=z//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
You can use list slice for this problem
num = 1234
reverse = int(str(num)[::-1])
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
int(str(num[::-1]) is nothing but converting the string format to integer .

input() function not working in Sublime text [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have the following function:
years=2
double_rate=(years*12)//3
number_of_rabbits=11
answer=number_of_rabbits*double_rate
print(answer)
I want to generalize the code by being able to input years and number_of_rabbits variables
years=input("select number of years: ")
print(years)
double_rate=(years*12)//3
number_of_rabbits=input("select number of rabbits: ")
print(number_of_rabbits)
answer=number_of_rabbits*double_rate
print(answer)
However the editor (sublime text) only prompts me for the first input variable. I am not able set the second one, "select number of rabbits", nor does it print the new answer
Does anyone know why this is the case?
The number of years you enter is saved as a string, not an int (or float). So when you try to calculate double_rate, you're multiplying a string by 12 (which is fine) and then floor dividing the result by 3, which doesn't work.
Try years = int(input("Select number of years: ")) instead.

I'm trying to learn python and my code doesn't work and i don't know why [duplicate]

This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 2 years ago.
whats my mistake?
it doesn't show you are right
print("what's the correct number?")
print("1/2/3/4/5/6/7/8/9")
correctNumber = 6
inputNumber = input()
if inputNumber == correctNumber:
print("you are right")
You can try
correctNumber = 6
inputNumber = int(input("what's the correct number? 1/2/3/4/5/6/7/8/9"))
if inputNumber == correctNumber:
print("you are right")
The problem was that input is store in a variable as a string and you need to cast it to int to compare it with int since correctNumber = 6 is int.
By the way, the input method expects a string to print the user before his input so you don't need to use print statements to notify the user about the expected input.
When using input(), you get a string that you compare int () and str (). to get a number in water you need int(input())

Why does this if statement not work correctly? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
This is supposed to be a random number guessing game. It is supposed to say whether the number was guessed correctly or not at the end but it will always display "Wrong" even if the number has been guessed correctly.
I've only been learning python for a couple of days and this is doing my head in.
import random
odds = [1,2]
rNum=(random.choice(odds))
print ('The odds are:',odds)
gNum=input('What is your guess?')
print ('You Guessed:',gNum)
print ('The number was:',rNum)
if (rNum == gNum):
(print("Correct!"))
if (rNum != gNum):
(print("Wrong"))
Its as if it can't compare if the number is the same or not and always interprets it as wrong, as it only will give the != result
Forgive me if I sound foolish I am only beginning
Many thanks :)
I imagine the types are different. One is an int, the other a str which happens to contain an integer. Try"
gNum=int(input('What is your guess?'))

Categories