(Beginner) Why is this not correct? - python

#Add the input number 4 times. Ex. 3+3+3+3
#If the input is 3, the output will be 12
num = int(input("Num: "))
for x in range(2):
num += num
print(num)
Using an app called "Easy Coder" and
for some reason, the code above is not correct.
Is there a better way to do this? so the actual process of the code is("3+3+3+3) and not(3+3) + (3+3)
Edit: Sorry, I forgot to mention, that this is an exercise related to
looping.
The task:
Write a program that uses a for loop to calculate any
number X 4.
Hint - Add the number to a variable 4 times.

To add 4 times, use a separate variable for the total and loop 4 times instead of twice
num = int(input("Num: "))
total = 0
for _ in range(4):
total += num
print(total)

You can always create another variable to store the total, or you could simply multiply the number initially by 4 instead of using a loop. Otherwise, like you said, your code is actually doing:
3 + 3 = 6
6 + 6 = 12
since you overwrite the initially supplied number.

Related

Ask user to enter a positive integer n and use this number to calculate the sum of series up to n term

Ask user to enter a positive integer n and use this number to calculate the sum of series up to n term. (20 pts)
Test case:
if n = 2 the series will become 3 + 33 = 36
if n = 5 the series will become 3 + 33 + 333 + 3333 + 33333 = 37035
The trick is to think about the number of times each digits place is added.
For instance, at n=5, you are adding the 3 in the units place 5 times, but adding the 3 in the ten thousands place only once. Because of this, it is appropriate to start the for loop at 5 and decrement as we add everything up together.
n = input("Please enter a number. ")
total = 0
for i in range(int(n),0,-1):
total += 3*i*(10**(int(n)-i))
print(total)
I am using math.pow to go to the next place (i.e. 1, 10, 100, etc.). I do int(n)-i because I want to start at 0 and go up to n for this exponent.
Output:
Please enter a number. 5
37035.0
Edit: I changed syntax to use Python's syntax for exponents instead of math.pow
You can use a recursive function that adds a 3 at the end using 10x + 3:
def function3(seed):
if(seed==1):
return 3
else:
return (10*f3(seed-1))+3
seed = int(input('integer please: '));
print(function3(seed))
We could also build a string which could be regarded as cheating if we're talking about a function.
Here is an alternative definition for the function:
def function3(seed):
s = ""
for i in range(0,seed):
s+="3"
return s

adding 2 integers from a list in python

im doing this exercise basically to add the first 2 integers in a list in python, but my script shows it runs the for loop twice before it iterates to the next integer in a list.
my code is below, i added a print statement so i can see how it iterates the FOR loop inside a WHILE loop.
def add2(nums):
count = 0
ans = 0
while count <= 2:
count += 1
print(f'count: {count}')
for x in nums:
ans += x
print(f'ans: {ans}')
HOwever if i run my code, i get this result. Why is it adding the value of ans twice before it goes to the next iteration?
add2([2,3,5])
count: 1
ans: 2
ans: 5
ans: 10
count: 2
ans: 12
ans: 15
ans: 20
count: 3
ans: 22
ans: 25
ans: 30
You don't need to overcomplicate this. Just use slicing to return the first to elements of the list.
simpler code
listNums = [1,2,3,4]
print(float(listNums[0])+float(listNums[1]))
output
3.0
This is based on your explanation of the problem.
Now using your logic of solving this proble, I would consider removing the while loop altogether but keeping the for loop. Though keeping the plain for loop will not give us our desired output because it will find the sum of every number in the list. Instead we can cut the list of at two elements. The below code shows how to do that.
your logic code
def add2(nums):
count = 0
ans = 0
for x in nums[:2]:
ans += x
print(f'ans: {ans}')
add2([1,2,3,4])
output
ans: 3
It could be as simple as this:
ans=0
for i in range(len(nums)):
ans+=nums[i]
if i > 2:
break

How do I use nested loops but increase value by 2 every time it loops

I am begginer and was just doint some basic excercies in Python. Basically I need to print following sequence of numbers:
This is what the multiplication table is up to 5 times 5.
↳
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Write a program that displays a multiplication table up to 20 times 20.
Also so far I have only learned loops and how to nest them so I think I need to solve problem using nested loops.
i=1
for loop in range(20):
for loop in range (20):
print(i, end = " ")
i=i+1
print()
for loop in range(20):
print(i, end = " ")
i=i+2
I have tried this but prints sequence of number that continues instead to start from 1 every time. I am sure I need to use loop where my value is incrementing by 2 every time that loops but I'm not sure how. Also please if you can give me short explination on the solution. Thank you
you can try:
for i in range(1, 21):
for j in range(1, 21):
print(j * i, end = " ")
print()
Another way of doing it, closer to what you tried to do:
for i in range(1,11): #number of rows you want to print
for j in range(i,i*5+1,i): # go from i to i*5 with a step of i
print(j,end=' ')
print()

print the out put of python program based on Permituation and combination

I write a program in python to find no of combinations of series
there is one dice which has 6 faces.
user input 2
then the out is shown is as no of count where two is come in combinations
ex if we throw dice for to get 2 as sum the maximum are two dice thrown are required
(1+1) and (2) so count is 2
if i throw for sum of 3, the out-put is
(1+1+1),(1+2),(2+1),(3) so count is 4 enter code here
if i throw for sum of 4 then the out put is
(1+1+1+1),(1+1+2),(1+2+1),(2+1+1),(2+2),(3+1),(1+3),(4) count is 8
I write the code is
# I am considering the Board is horizontal single line
def count_values(values,num):
for i in range(num):
print(values[i]," ",end='')
print('')
def print_list(out_put,values,num,count=0,show=False):
dice=6
if num == 0:
count_values(values,count)
out_put[0] += 1
elif num > 0:
for k in range(1,dice+1):
values[count] = k
print_list(out_put,values,num-k, count+1,show)
n=int(input('Enter A number'))
values=[0]*n
out_put=[0]
print_list(out_put,values,n)
print(out_put)
it shows out put for small inputs likes 10,20,30
but
i want the out put for 100 and 500 and 610 like inputs ,
but is get more time (around 5-6 hours still running) and the count of combination is more than 1145201564
still it is counting
any one has solution for this
Any one has any solution. for this
import numpy
def findway(m,n,x):
table = numpy.zeros((2,x+1))
for j in range(1,x+1):
table[1][j] = 1
for j in range(1,x+1):
for k in range(1,j):
table[1,j] += table[1][j-k]
print table[1][x]
n=input('Enter a number')
findway(6,1,n)
but here is also one problem, i want out put for 600 ,but it shows out-put in format (2.07475778444e+180)
but i want in integer format

Create a search in a range

I am new to Python and would like a simple code to be executed however due to my understanding of the syntax I am not able to create it.
I would need to create a range between 1 to 10 and create an input function to search if this number is within this range. My code looks like this:
range=[1,10]
i=0
for i in len(range):
if (i) > 1 and (i) < c:
print ("HH")
However there is an error. Any solutions with explanations?
range is a function so you can either use
if i in range(1,10):
or
if i >= 1 and i < 10:
In your code, you made a variable that happened to be named range but it was actually just a list with two int elements in it.
The following might help you understand a bit better. First of all avoid calling your variable range as this is a Python function. It is used to return a list of numbers. It can take two parameters, a starting number, and an ending number. The ending number is not included in the list, so for 1 to 10 you need range(1,11).
c = int(input("Enter search number: "))
for i in range(1, 11):
if 1 < i < c:
print(i, "HH")
else:
print(i)
This would display:
1
2 HH
3 HH
4 HH
5
6
7
8
9
10
I am not sure which version of Python you are using. If it is earlier than Python 3, then remove the brackets from the print statements.
If you just want to see if the entered number is in a certain range, something like the following would be suitable:
c = int(input("Enter search number: "))
if 1 <= c <= 10:
print("The number {} is in the range 1 to 10".format(c))
else:
print("The number {} is not in range".format(c))
You would then see it display:
Enter search number: 5
The number 5 is in the range 1 to 10

Categories