Reading ints in input in a Python program [closed] - python

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 months ago.
Improve this question
I have to take input from the user then the operator has to print what number is before the input and after the input like:
input= 3
has to print 2 and 4
I used range, did I do it wrong? I am just a beginner in Python.
number=int(input)
for num in range(number ,+ 1, -1):
print(num)

You first need to use input() to let the user register a number.
Then, simply print the number with number - 1 and number + 1.
number = int(input("What is your number? "))
print(f"{number - 1} {number + 1}")
Outputs to:
What is your number? 3
2 4

you don't need range do this task
num = int(input())
print(num-1, num+1)

Other answers which say that you don't require a loop are perfectly fine, however I want to add a little suggestion in case you need to print more than just the number before and after. Maybe (as your first implementation suggests) you want to print the 5 numbers before and after. In this case you can do:
R = 5 #range
N = int(input("Enter your number. "))
numbers = [ i for i in range(N-R,N+R+1) if i != N]
print(numbers)

Related

I am Wrote this python code but every time it's give me wrong answer [closed]

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 2 years ago.
Improve this question
I have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)

I wrote some code for find an armstrong number , I dont know why it doesnt give me false output? [closed]

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 2 years ago.
Improve this question
A number that equals to the sum of its own digits, where each digit raised to the power of number of digits. For example, 153 is an armstrong number
because 1^3+3^3+5^3=153
at here user enters a number
number=int(input("please enter a number: "))
Here in a while loop it puts the digits of the given number in numbers class
numbers=[]
while number>0:
rem=number%10
numbers.append(rem)
number=int(number/10)
and then we want to put their qubes in qubes class
qubes=[]
for i in range(0,len(numbers)):
c=(int(numbers[i]))**len(numbers)
qubes.append(c)
and now we calculate the sum of the qubes class members
result = sum(i for i in qubes)
I dont know why the if_code below doesnt work it just gives me false output I dont know why??
even when i enter 153 it prints false
if result==number:
print("true")
else:print("false")
To sum-up all suggestions from the comments:
Your main problem is:
When you create the numbers list you use number = int(number/10). This changes the number variable itself until it is equal to zero. This means that, as you experienced, result == number will always be False.
Some redundant parts of your code:
See Splitting integer in Python? to get a list of a number's digits. Most commonly you can just do numbers = [int(i) for i in str(number)]. This will actually solve the problem above as you don't change number this way.
The digits are already integers so no need for an int conversion. It is also more readable to use direct loop in Python rather than looping over indices:
qubes = []
for num in numbers:
qubes.append(num**len(numbers))
sum(i for i in qubes) is just an overly explicit way of saying sum(qubes).
You are printing either "true" or "false" according to a boolean result. There is no need for a condition and you can simply print(result == number).
Putting together all the above, you can achieve this with a single line of code:
print(number == sum(int(digit)**len(str(number)) for digit in str(number)))
This is where you are wrong. you should iterate over the integer not the iterator given by range function
Use this
qubes=[]
for i in numbers:
c=(int(i)**len(numbers))
qubes.append(c)
instead of
qubes=[]
for i in range(0,len(numbers)):
c=(int(numbers[i]))**len(numbers)
qubes.append(c)
Also you are referencing the number to itself number=(int(number/10)) so your result value will be 153 but number value will be 0 at the end because you have reduced the value of number. So copy the value of number to another variable (num1 in below code).
Full code:
number=int(input("please enter a number: "))
num1 = number
numbers=[]
while number>0:
rem=number%10
numbers.append(rem)
number=int(number/10)
qubes=[]
for i in numbers:
c=(int(i)**len(numbers))
qubes.append(c)
result = sum(i for i in qubes)
print(result == int(num1))

Why this Syntax Error on Python 3.8 for Mac [closed]

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 2 years ago.
Improve this question
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)

python - a collatz program automate the boring stuff [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Hi all I have read through the previous answers to this question and can get the code to run. What I want to understand is why my code doesn't run.
Thanks
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
print('Enter a number')
number = int(input())
while number != 1:
print(int(collatz(number)))
You are not updating number in your while loop so you are stuck in an infinite loop.
You should assign return value of collatz to number back, to update number.
while number != 1:
number = collatz(number)
print(number)

Can't convert 'tuple' object to str implicitly [closed]

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 8 years ago.
Improve this question
a=int(input("please input number of players "))
while (a < 2):
a=int(input("please input at least two players "))
if (a==2):
p1=input("please enter name for player 1 ")
p2=input("please enter name for player 2 ")
x=float(input("please enter initiative for "+(p1)))
y=float(input("please enter initiative for "+(p2)))
x=x,p1
y=y,p2
if (x > y):
lowest=y
highest=x
elif(y > x):
lowest=x
highest=y
print(lowest)
steps=int(input("Please enter number of steps for "+(lowest)+" action" ))
i have tried everything i can think of and it needs to do this but i can't figure out how to make it work
That's exactly what the message say: in the last line, in the subexpression
"Please enter number of steps for "+(lowest)+" action"
you are trying to concatenate a string with a tuple (lowest - since it is either x or y, and both of them are tuples due to the assignment x=x,p1 and y=y,p2), which would require an implicit conversion from tuple to string.
To fix this, you have to convert explicitly the tuple to string (str(lowest)) (although I suspect that you actually want just one of the elements of the tuple to be displayed).

Categories