Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm currently studying Python and I've been stuck at this problem for a few hours now. I was given a task to take user input of numbers and split those numbers, and display numbers greater than 3, example:
Input: 12345
Output: 4
5
I had some progress with splitting those numbers, and now I'm at this part where it's supposed to figure out if the number is greater than 3 but I keep on getting "TypeError: '>' not supported between instances of 'list' and 'int' "
Anyway, here's my code:
num_input = list(input('Enter number: '))
for i in num_input:
if num_input > 3:
print(i)
Ok, it should be:
num_input = input('Enter number: ')
for i in num_input:
if int(i) > 3:
print(i)
Basically, you just get a single char from the string 12345 at a time and convert it to an int. Then you can compare it to 3 normally.
If you need a shorter version:
num_input = input('Enter number: ')
print([int(i) for i in num_input if int(i) > 3])
Which you output: [4, 5] if you input 123456
You have to make the objects inside the list as integers and then only you can compare them to an integer, in your case greater than three. This should work
num_input = list(map(int,input().split()))
for i in num_input:
if i > 3:
print(i)
This allows to input multiple values in a single line
You don't need the list(). Your input is string, you can iterate through every character of string, cast it to int and then compare:
num_input = input('Enter number: ')
for i in num_input:
if int(i) > 3:
print(i)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
Hi I'm trying to solve this coding homework:
Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.
I define the while loop to make sure it keeps asking, as:
while n != -1
str(input("enter your number:"))
But whenever I try to input -1, it just keeps on asking to enter the number regardless.
Also, I'm not sure what is the best way to define the average excluding -1, none of the lessons prior to this assignment talked about this. I have Googled about it but none of the examples match this particular assignment, even fumbling around did not help.
Thank you for your help :)
Presumably n is meant to be the user input, but you're never assigning a value to n. Did you mean to do this?
n = str(input("enter your number:"))
Also, you're comparing n to -1, but your input isn't a number; it's a string. You can either convert the input to a number via n = int(input(...)), or compare the input to a string: while n != '-1'.
You could ask for a number the if it is not equal to -1 enter the while loop. So the code would be:
n = float(input("What number?"))
if n != -1:
sum += n
nums_len = 1
while n != -1:
sum += 1
nums_len += 1
n = float(input("What number?"))
print("The average is", str(sum/nums_len))
Thanks everyone, this is the final code with the correct values that gives the average of user inputs
n = float(input("What number?"))
if n != -1:
sum = 0
nums_len = 0
while n != -1:
sum += n
nums_len += 1
n = float(input("What number?"))
print("The average is", float(sum/nums_len))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
This code verifies if number is in the list.
n = input('enter number: ')
list = [1,2,3,4,5,6,7,8,9,10]
if n in list:
print(True)
print('position')
else:
print('try again')
How to find position of the number in the list using binary search?
You need to convert n to an integer otherwise it won't be found in the list:
n = int(input('enter number: '))
>>> list = [1,2,3,4,5,6,7,8,9,10]
>>> "1" in list
False
>>> 1 in list
True
Also to get the index, use list.index(n)
You can use list.index(item) function which returns the zero-based index of first match of item in the list if it exists, else raises ValueError if it does not exist.
n = 10 #
n = int(input('enter number: '))
list = [1,2,3,4,5,6,7,8,9,10]
if n in list:
print(True)
print(list.index(10))
else:
print('try again')
output -
True
9
As pointed out by #Cubix48, you need to also cast your input into int as well before doing the matching since it will be of str type otherwise.
You should avoid built-in types(list) as names of your variables.
In the updated question, you want to find the index using binary search. This will only work your list is sorted already, you can use the bisect-left which would return the insertion point for item in list to maintain sorted order.
from bisect import bisect_left
n = 10
my_list = [1,2,3,4,5,6,7,8,9,10]
print(bisect_left(my_list, n))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a python code in which I input a number, which should be a multiple of 3, and calculate the sum of each digit's power of 3,
ig: input'12' => multiple of 3
should return 1^3 +2^3 = 9
I tried this script and didn't get anything when I run it, what's the problem
number= input('give a number: ')
TT=list(number)
if int(number)==0:
print('wrong number')
elif int(number)%3:
for x in (range(len(TT))-1):
aggregate=sum(int(TT[x])**3)
print(f'the aggregate of {number} is {aggregate}')
None of the conditions are true, so no value ever gets displayed. If you input 12, then 12 % 3 equals 0 (False).
Take a look at the python truth value testing
You should update this line:
elif int(number)%3 == 0:
Now, the whole code needs a revision to get the result that you want. Let me make some changes:
number= input('give a number: ')
aggregate = 0
if int(number)==0:
print('wrong number')
elif int(number) % 3 == 0:
for x in number:
aggregate += int(x) ** 3
print(f'the aggregate of {number} is {aggregate}')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to insert user input into a list until the user enters '0'. Right now, it won't exit out of the while loop once I enter 0.
Here's my code:
num_array = []
inputnum = raw_input('Please enter a number: ')
num_array.append(inputnum)
while inputnum != 0:
inputnum = raw_input('Please enter a number: ')
num_array.append(inputnum)
for i in range(len(num_array) - 1):
print(num_array[i] + ' + ')
print(num_array[total - 1] + ' = ' + sum(num_array))
Try check:
num_array = []
# raw_input return value as string,int convert it to string.
inputnum = int(raw_input('Please enter a number: '))
num_array.append(inputnum)
# earlier check for string '0' to interger 0 so the condition returned true but now int() converted inputnum to integer.
while inputnum != 0:
inputnum = int(raw_input('Please enter a number: '))
num_array.append(inputnum)
for i in range(len(num_array) - 1):
print("%d +" % num_array[i])
# sum() gave exception because earlier num_array had string type but int() converted all the value to integer.
print("%d = %d" % (num_array[len(num_array) - 1],sum(num_array)))
Try using
while inputnum != '0':
because the raw_input function returns a string, not integer. However, if your input_num needs to be an integer then follow the above solution.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
It should print out stuff stored in generator variable if raw input was 1, but its not doing that, its executing else statement even if I write 1.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if x == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'
You're comparing a string (what was input) with an int. Try if x == '1' instead.
Turn's answers is a good one, but there is an alternative way to do this by using int(). The built-in function int() treats the string as an int.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if int(x) == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'