Comparing numbers give the wrong result in Python - python

If I enter any value less than 24, it does print the "You will be old..." statement. If I enter any value greater than 24 (ONLY up to 99), it prints the "you are old" statement.
The problem is if you enter a value of 100 or greater, it prints the "You will be old before you know it." statement.
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

You're testing a string value myAge against another string value '24', as opposed to integer values.
if myAge > ('24'):
print('You are old, ' + myName)
Should be
if int(myAge) > 24:
print('You are old, {}'.format(myName))
In Python, you can greater-than / less-than against strings, but it doesn't work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)
>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True
You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) -- You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + -- You can read more about it in the docs. But it really doesn't have anything to do with your core problem.

The string '100' is indeed less than the string '24', because '1' is "alphabetically" smaller than '2'. You need to compare numbers.
my_age = int(input())
if my_age > 24:

print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;
Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).
Python Strings Tutorial

Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:
myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

Related

How do I add the sum of two inputs?

Hello everyone I am very new to Python I am taking a beginner course at my college, and I am stuck on one portion of my project. Basically the goal of my code is to produce the output so that when I enter any age and name the result is supposed to find the sum of the age and number of letters in the entered name. So far this what I have typed up.
print('What is your name?')
myName = input()
print('What is your age?')
myAge = input()
sum = myAge + str(len(myName))
print(myName + ', if we add the number of letters in your name to your age then you will be ' + sum + ' in ' + str(len(myName)) + ' years.')
When I run the script all I get is the age+length of name which gives me a combination and not a sum. example 21 + 4 = 214.
Basically my issue with this is that I don't understand how to find the sum of both inputs so that I get result of adding age and the length of letters in a name. The last portion that I am trying write should in other words be this "Name, if we add the number of letters in your name to your age then you will be # in # years."
If anyone can explain to me how I can accomplish this then I would greatly appreciate it I have spent hours looking into this issue but can't figure it out.
When you use input() it returns a string.
So when setting it to sum, you end up with "21" + "4" (which will result in a string) -> "214"
Try converting myAge to an int like so
sum = int(myAge) + len(myName)
This will make it where you add 21 + 4 (now both numbers), so you'll get 25
Result of "input()" always is a string. You need to convert your input to integer using:
myAge = int(input('What is your age?'))
During the sum of the two variables should be converted to integers or decimals
print('What is your name?')
myName = input()
print('What is your age?')
myAge = input()
sum = float(myAge) + float((len(myName)))
print(myName + ', if we add the number of letters in your name to your age then you will be ' + str(sum) + ' in ' + str(len(myName)) + ' years.')

Method gives diffrent output with loop [duplicate]

If I enter any value less than 24, it does print the "You will be old..." statement. If I enter any value greater than 24 (ONLY up to 99), it prints the "you are old" statement.
The problem is if you enter a value of 100 or greater, it prints the "You will be old before you know it." statement.
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
You're testing a string value myAge against another string value '24', as opposed to integer values.
if myAge > ('24'):
print('You are old, ' + myName)
Should be
if int(myAge) > 24:
print('You are old, {}'.format(myName))
In Python, you can greater-than / less-than against strings, but it doesn't work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)
>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True
You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) -- You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + -- You can read more about it in the docs. But it really doesn't have anything to do with your core problem.
The string '100' is indeed less than the string '24', because '1' is "alphabetically" smaller than '2'. You need to compare numbers.
my_age = int(input())
if my_age > 24:
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;
Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).
Python Strings Tutorial
Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:
myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

3 is bigger than 10 according to python [duplicate]

If I enter any value less than 24, it does print the "You will be old..." statement. If I enter any value greater than 24 (ONLY up to 99), it prints the "you are old" statement.
The problem is if you enter a value of 100 or greater, it prints the "You will be old before you know it." statement.
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
You're testing a string value myAge against another string value '24', as opposed to integer values.
if myAge > ('24'):
print('You are old, ' + myName)
Should be
if int(myAge) > 24:
print('You are old, {}'.format(myName))
In Python, you can greater-than / less-than against strings, but it doesn't work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)
>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True
You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) -- You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + -- You can read more about it in the docs. But it really doesn't have anything to do with your core problem.
The string '100' is indeed less than the string '24', because '1' is "alphabetically" smaller than '2'. You need to compare numbers.
my_age = int(input())
if my_age > 24:
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;
Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).
Python Strings Tutorial
Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:
myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

How to make guessing the number game(high, low hints) with cheating allowed (Ulam's Game) in python?

I made a game of guessing the number where computer choose a secret number between 1 to 100 and i try to guess that number. On each guess, computer gives hint that whether it is lower or higher than his secret number by saying "too low" or "too high". But i want that sometimes computer will cheat ( for example in place of saying "too low" it will say "too high"). But whenever he tells a lie, in very next guess he must have to tell the truth. He must not to be allowed to lie in two consecutive guesses. Though he can lie alternatively. He may tell the truth without any limitation, consecutively and anytime. In below code, computer randomly say "too low" or "too high". By doing so he is sometimes lying consecutively more than once. Please fix the code so that it doesn't lie in two consecutive guesses.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if guess < number:
print('Your guess is too ' + random.choice(result) + '!')
if guess > number:
print('Your guess is too ' + random.choice(result) + '!')
if guess == number:
break
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
Here is how I solved the problem.
We define a variable can_lie. It tells if the computer is allowed to lie, it's True if the last thing he told was the truth.
Then if we are allowed to lie, and only one time over two, we lie.
After this we are not allowed to do it for the next turn, so can_lie become False.
Then we say the opposite of what we should have said, because we are lying.
If we are telling the truth, then can_lie become True, so we are allowed to tell whatever we want the next turn.
Then we tell what we are supposed to say.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
can_lie = True
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if can_lie and random.choice((True, False)):
#Here we lie
can_lie = False
if guess > number:
print('Your guess is too low !')
if guess < number:
print('Your guess is too high !')
else:
can_lie = True
if guess < number:
print('Your guess is too low !')
if guess > number:
print('Your guess is too high !')
if guess == number:
break
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
This code lies randomly, but never twice in a row.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
previous_prediction = True
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if guess == number:
break
if previous_prediction:
low_high = random.choice(result)
else: # Make random choice only if told truth in previous choice
low_high = 'low' if guess < number else 'high'
print('Your guess is too ' + low_high + '!')
#If there is a lie between actual result and told result, mark the prediction as False, such that next time, only truth is provided
if not (( (guess < number) and low_high=='low') or ( (guess > number) and low_high=='high')):
previous_prediction = False
else:
previous_prediction = True
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
However, you should have tried a bit more to solve it.

What is the Syntax error in this code that detects an even or odd number in python?

Here is the code that the syntax is on:
# Odd or Even?
print('Hello! What is your name?')
PlayerName = input()
print("Hello, " + PlayerName + "! Enter your number and I\'ll tell you if it\'s even or odd!")
PlayerNum = input()
Decimal = PlayerNum % 2
if Decimal == 0.5
print('Your number is odd!')
else
print('Your number is even!')
It gives syntax on line 7 (if Decimal == 0.5). Let me know if there are any other errors. I'm also using python 3. What's the problem?
Thanks
Your code should look like this:
print('Hello! What is your name?')
playerName = input()
print("Hello, " + playerName + "! Enter your number and I\'ll tell you if it\'s even or odd!")
playerNum = int(input())
if playerNum % 2 == 0:
print('Your number is even!')
else:
print('Your number is odd!')
You need to insert a colon after if and else conditions.
The input is read as a string, you need to convert it to int or float
You need to indent your code (I recommend 4 spaces)

Categories

Resources