Can someone explain to me the part which is highlighted? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Im already done with the part before the highlighted part, but i can't understand the part which is highlighted and i dont want to look at the code.

Something like that:
number = int(input("Your number: "))
while number != 1 :
number = collatz(number)

As I understand, it should be something along the lines:
def collatz(number):
if number // 2 == 0:
print(number // 2)
return number // 2
else:
print(3 * number + 1)
def iterate_program:
number = int(input())
while number != 1:
number = collatz(number)
And then you just call iterate_program.

Related

why am i getting the wrong answer for the index of the number whos left sum=right sum in the list [closed]

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 1 year ago.
Improve this question
please tell my mistake
i m getting the index of the number whose leftsum=rightsum wrong
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(0,n):
if sum(range(L[0],L[i])) == sum(range(L[i+1],L[-1])):
print(L.index(L[i]))
break
else:
print("0")
For starters, you've used range(a,b), which returns all the numbers from a to b-1
Try this:
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(1,n):
if sum(L[0:i]) == sum(L[i+1:]):
print(L.index(L[i]))
break
elif i==n-1:
print(0)

Input multiple user inputs into an array / list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Sorry if it's an easy question. But what I need to do is have a script which inputs 5 numbers into an array. For instance the user will be asked 5 times to input numbers from which the mean and sum will be calculated. Is placing the users inputs into an array the easiest way to do it and if so how?
counter = 0
number_array = []
while counter < 5:
num = float(input("Enter a number: "))
number_array.append(num)
counter += 1
total = 0
for number in number_array:
total = total + number
average = total / 5
print("The sum is: ", total)
print("The average is ", average)

Receiving integers from the user until they enter 0 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It's my 2nd week in programming in Python and have never programmed anything before. appreciate step by step.
I don't know where to start.
try:
count = 0
while True:
user = int(input('Insert Number: '))
count += user
if user == 0:
break
print(count)
except ValueError:
print('Please Insert Numbers Only!')
Here a start, use a while loop for the input. I'll leave the summation part for you unless you need further help:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
while cc != 0:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
def is_int(num):
try:
return int(num)
except:
return False
total = 0
while True:
in_user = is_int(input("input integer: "))
if in_user is not False:
total += in_user
if in_user is 0:
break
print(total)

Print a pyramid of numbers in python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Okey, I actually don't really know how to print a pyramid in python and even worse I need to get this:
Pyramid of n numbers
The length of the pyramid is asked to the user
Here's the code.
n = int(input("\n Enter the lenght of the triangle.. "))
for i in range(n):
for j in range(n - i):
print(" ", end = " ")
for k in range(i):
print("*", end = " ")
print()
Here is the output
One of the options:
import numpy as np
a = int(input())
def pyram(a):
n = np.zeros(a)
hal = int((a+2)/2)+1
n[:(hal-1)] = range(1,hal)
n[(hal-1):] = np.array(range(1,hal-1))[::-1]
print(n)
for i in range(1,a*2,2):
pyram(i)

Need Python Help for increasing Numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
so from this code, that is writen down below, if k is even i must +3 to K number, if it's odd i must multiply it 5 times, i dont know how to show that if k is even, anyone can help me out?
k=int(input('K= ')
if k%2: # (is this correct? if k is even)
k+=3
else:
k*=5
In python3,
k = int(input())
if k%2 == 0:
k = k + 3
else:
k = k * 5
if you are using python2, use raw_input() to take input from the user.
hope this helps.
I think you should really study some basic books about python.
You see, blocks work by indenting in python; and else needs a ":" too:
if k % 2 == 0:
k = k+3
else:
k = k*5
should work better.

Categories