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.
Related
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())
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Python: How to keep repeating a program until a specific input is obtained? [duplicate]
(4 answers)
Closed 3 years ago.
I want to get a user input which is bigger than zero. If user enters anything else than a positive integer prompt him the message again until he does.
x = input("Enter a number:")
y= int(x)
while y<0:
input("Enter a number:")
if not y<0:
break
If I add a bad value first then a good one it just keeps asking for a new one. Like Try -2 it asks again but then I give 2 and its still asking. Why?
The first time you assign the result of input to x, and convert x to a number (int(x)) but after the second call to input you don't. That is your bug.
That you have a chance to get this bug is because your code has to repeat the same thing twice (call input and convert the result to a number). This is caused by the fact that Python unfortunately does not have the do/while construct, as you mentioned (because there was no good way to use define it using indentation and Python's simple parser, and now it's too late).
The usual way is to use while True with break:
while True:
x = input("Enter a number:")
y = int(x)
if not y<0:
break
You assign a value to y before the while loop, then you enter the loop without assigning a value to y from the input.
Just change your code:
x = input("Enter a number:")
y= int(x)
while y<0:
y = int(input("Enter a number:"))
print('out?')
This question already has answers here:
Numpy is calculating wrong [duplicate]
(2 answers)
Closed 3 years ago.
I am attempting to solve problem 8 of projecteuler. I am having difficulty understanding exactly why my code is not outputting the correct solution. I am aware that the solution to this problem is above the 32 bit maximum, but I do not know how to allow python to work with numbers above that within my code.
For reference, the original question states : "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
from numpy import prod
f = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
z = list(int(i) for i in str(f))
a1 =[]
start = 0
end = start + 13
while end <= len(z):
a1.append(prod(z[start:end]))
start+=1
end+=1
a = a1.index(max(a1))
print(a1[a]) #prints the product solution
print('---')
dimlen=end-start
newstart = a
newend=a+dimlen
print(z[newstart:newend]) #prints the integers that build the solution
I keep getting the number 2091059712, (the solution is 23514624000)
I think it might be numpy.prod. It might be preserving the input type and wrapping the value. Try using:
def prod(it):
p = 1
for m in it:
p *= m
return p
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I'm beginner in python but I don't understand a thing.
This is the Code:
a = input("Insert first number ")
b = input("Insert second number ")
c = input("Insert third number ")
print("Max number is", max(a, b, c))
For example, I write at prompt:
Insert first number 12
Insert second number 34
Insert third number 100
Max number is 34
I don't understand! Please answer me!
Input returns string and strings are compared lexicographically. You should cast all input results to int, to get numbers.
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).