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 6 years ago.
Improve this question
I have this pseudocode that I need to translate:
Prompt the user to enter a string and call it s.
Let l be the length of string
For i from 0 upto l-1:
print s[0:i]
For i from 0 upto l-1:
print s[i:l]
Print a closing statement
This is my translation:
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in [0,L-1]:
print (s[0:i])
for i in [0,L-1]:
print(s[i:L])
print("This program is complete!")
main()
However, the code isn't printing correctly. Can someone help me find my error? Thank you.
You say for i in [0,L-1], but [0,L-1] is a list with two elements: 0 and L-1. What you want instead is range(0, L) or range(L):
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in range(L):
print (s[:i])
for i in range(L):
print(s[i:L])
print("This program is complete!")
main()
Related
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.
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)
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 3 years ago.
Improve this question
a = int(input('Enter the number:'))
list=[]
for i in range(1,a+1):
if (a%i==0):
list.append(i)
print(list)
print(sum(list))
Here in this code, I tried to find out the divisor of a given number, then print the divisors and lastly the sum of the divisors. What I need is to put this as a function to call it for future uses.
You could try something like this:
def my_func(val):
lst = []
for i in range(1,val+1):
if (val%i==0):
lst.append(i)
return lst, sum(lst)
Now you can simply call your function like this:
a = int(input('Enter the number:'))
lst, lst_summ = my_func(a)
print(lst)
print(lst_summ)
Also, it is not good practice to use built in functions as variable names. list is a built in function.
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)
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 7 years ago.
Improve this question
I wrote two functions. First one, when called checks if the number is three-digit, and second one is checking if the number is even or odd:
def even(n):
if n%2==0:
print('number {} is even'.format(n))
else:
print('number {} is not even'.format(n))
return
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return True
else:
print('I said three-digit number!! ::')
return False
main(0)
What my problem is, when I call function even like even(a), it gives me an error saying a is not defined.
You must return a in the main() function and pass it to the even function.
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return a
else:
print('I said three-digit number!! ::')
return False
a = main(0)
even(a)
You have to return the value of a so you can use it somewhere else
Also you have a while and return in if and else.
Therefore you do not need your while loop. Or you want the user to reenter on error you then have to get rid of the return in else
You should give your function a more meaningfull name like read_threedigit_number
You normally don't put paranthesis around if and while
Improved Code
def read_threedigit_number(n):
while True:
a=int(input('three-digit number pls: '))
if a>99 and a<1000:
return a
else:
print('I said three-digit number!')
a = read_threedigit_number(0)
even(a)