This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
I know the names are kind of long but I wanted to make sure there was no way it was pulling from used variables
firstvalue = input("first")
secondvalue = input("second")
if firstvalue < secondvalue:
print ("first value is < second value")
print ( firstvalue, "less than", secondvalue)
else:
if firstvalue == secondvalue:
print ("first is = second")
else:
print ("first is greater than 2nd")
print (firstvalue , "greater than", secondvalue)
when i input 12 for the first value and 3 as the second value, i get 12 < 3
You're reading the variables as strings not integers, try this instead:
firstvalue = int(input("first"))
secondvalue = int(input("second"))
You are currently comparing strings and "12" is less than "3" because 1 comes before 3 in the ASCII table.
If you wish to compare the int value of the inputted string use int(input()) but then you may want to catch ValueError
When you input from commandline your values are text values. And remain such if you do not convert them to int or float.
When comparing text values python of course compares alphabetically and so the first character is compared to the first character. Which in your example are 1 and 3 and "1" is less than "3"
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=a>b
print("Is a greater than b ? ", c )
ISSUE is it showing the opposite output always like the when you enter a greater than b it showing flase and vice versa
your problem is that you compare strings instead of floats
since when you are comparing stings python compares the lexicographic value of the strings, that way "9" is grater then "12357645"
if you convert the input to float that should fix it :)
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=float(a)>float(b)
print("Is a greater than b ? ", c )
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.
This question already has an answer here:
Python - Numeric Literal expressions
(1 answer)
Closed 1 year ago.
input-print(1,000,000)
output-1 0 0
Try this:
numbers = "{:,}".format(5000000)
print(numbers)
What you have is equal to
print(1, 0, 0)
which is to print out three separate things: the number one, the number zero, and another number zero. For a decimal separator in python, you can use _, so if you try
print(1_000_000)
or just plain old
print(1000000)
But if you really want that ,, you need to print out a string instead. e.g.
print("1,000,000")
This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 3 years ago.
I wish to understand,
"apple" > 10 return True always.
I have mistakenly compared string with integer. instead of raising error it returns boolean.
I want to reason behind it..
When checking string with greater than Number it always return True.
eg 1: '' > 0 = True
eg 2: 'something' > 10 = True
etc, etc.
what it means actually?
I have tried, bytes of string, id etc. i am not sure what it means.
i can understand when if its string > string
here will get result based on sorting order something like below,
>>> 'a' >= 'a'
True
>>> 'apple' >= 'a'
True
>>> 'apple' > 'a'
True
>>> 'apple' > 'b'
Note: in Python 3 it will raises an error. what about python 2.x?
I know its sorting based. number has less precedence than string.
but, is that precedence is based on memory consumption?
I found this definition:
For python2:
"If the comparison is between numeric and non-numeric, the numeric (int, float) is always less than non-numeric and if the comparison is between two non-numeric it's done by lexicographical ordering(str) or alphabetical order of their type-names(list, dict, tuple)."
For python3:
It will return TypeError.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I really just can't understand why this code is not working...
It's probably just a typo. But even my more experienced friend is stumped here.
The prompt is simply "write a program that tells you how many 4s there are in a given list." Its all working except that count says zero no matter how many 4s I submit.
def num_four(number_list):
count = 0
print number_list
for i in number_list:
print i
if i == 4:
count = count + 1
return count
number_list = raw_input("Enter list of integers here: ")
print num_four(number_list)
the output looks like this:
Enter list of integers here: 123444
123444
1
2
3
4
4
4
0
raw_input returns a string like "8675409". When you iterate over this string (it's not a list), you get the string "8", then "6", then "7" -- it will eventually be "4", but never be the int 4.
The problem is that you don't pass a list of numbers to your function because raw_input returns a string.
number_list = raw_input("Enter list of integers here: ")
number_list = map(int, number_list.split())
print num_four(number_list)
This assumes that the numbers you input are separated by whitespace.
If you just put in strings like '1234', iterate over the string and check against '4' instead of the integer 4 in your function. If that's the case, consider renaming number_list to something more fitting like continuous_number_string.
And if you want to simplify your function, use the count method of either str or list:
>>> [1,2,3,4,4,5].count(4)
2
>>> '1234544'.count('4')
3
Please check the answer of #Mike Graham.
And just in case you are looking for a more Pythonic solution:
from collections import Counter
Counter(raw_input()).get("4")
raw_input will produce a string, not a list of integers, regardless of what you type in. So i will never equal 4 (integer) at most "4" (string).