I want to write a python programme that does repeated multiplication - python

A programme that asks the user to enter a number, multiply it by 2, and multiply the answer by 2, and so on, as many times as you want
Lets say the number is 100
Expected result
100×2= 200
200×2=400
400×2=800
800×2=1600,
and as many times i want

number = input("What number?")
iteration = input("How many times?")
for i in range(iteration):
new_number = number * 2
print(new_number)
number = new_number

Related

How do i create a number of inputs from an input

I'm brand new to this, 10 days in.
I've been thinking how I could solve this for 30 min. Please help.
Find Average
You need to calculate the average of a collection of values. Every value will be valid number. The average must be printed with two digits after the decimal point.
Input-
On the first line, you will receive N - the number of the values you must read
On the next N lines you will receive numbers.
Output-
On the only line of output, print the average with two digits after the decimal point.
Input
4
1
1
1
1
Output
1.00
Input
3
2.5
1.25
3
Output
2.25
From what I see, I figure I need to create as much inputs as the N of the first one is and then input the numbers Id like to average and then create a formula to average them. I may be completely wrong in my logic, in any case Id be happy for some advice.
So far I tried creating a while loop to create inputs from the first input. But have no clue about the proper syntax and continue with making the new inputs into variables I can use
a=int(input())
x=1
while x<a or x==a:
float(input())
x=x+1
a=int(input('Total number of input: '))
total = 0.0
for i in range(a):
total += float(input(f'Input #{i+1}: '))
print('average: ', round(total/a,2))
Modified a bit on your version to make it work
There were few things that you were doing wrong. when the numbers are decimals use float not int.
If you are looking for a single line input this should be how it's done.
When writing the code please use proper variables and add a string that's asking for the input.
total = 0
first_num=int(input("Number of inputs: "))
number = input("Enter numbers: ")
input_nums = number.split()
for i in range(first_num):
total = total + int(input_nums[i])
average = total/first_num
print(average)
If you are looking for a multiline output This should be how it's done.
first_num=int(input("Number of inputs: "))
x=1
total = 0
while x<first_num or x==first_num:
number = float(input("Enter numbers: "))
total = total + number
x=x+1
avg = total/first_num
print(avg)

Writing a program to divide user_num by x 3 times

I am trying to write a program for my class that divides the user_num by x three times. Thus far, I have:
user_num = input()
x = input()
So what is the missing link to do this? By three times, it should be like if the user inputs:
2000
2
Then the output should be:
1000 500 250
Am I on the right track and if so, what is the next step to make this work?
The first step is simply what you have, except the numbers are also converted into a numeric type to allow for mathematical operations.
user_num = int(input("enter a number: "))
x = int(input("enter x: "))
After that, you want to divide user_num by x, and then print the result, three times over. This is done using a loop. // will perform the division and ignore the decimal. print will work as expected, and the end argument will format the output as you described.
for _ in range(3):
user_num = user_num // x
print(user_num, end=" ")

Write a Python program to print the following sequence of numbers (N to be entered by user): X + X^3/3! + X^5/5!... Upto the Nth Term

The problem i am having with this question is that the code i have written is showing a logical error. Here is the code i have written:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,n+1,2):
factorial = factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
I used the for loop to only show odd values of index, because that is the requirement in the question. My output is coming as follows:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+
We don't have to actually find the sum, only display all the individual addends separated by a plus sign. What changes should i make within the code to get the required results?
My required output would look something like this:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+1.333333+0.266666+0.025396
Just change to
for i in range(1,2*(n)+1,2):
from
for i in range(1,n+1,2):
Now, the output is:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+2.133333+1.219048+
Also, the method of calculating factorial is wrong as it skips half of the terms, when you jump i from 1 to 3 to 5, so that 2, 4, 6.. are getting missed.
So, you can do:
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
So, the final code would be:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,2*n+1,2):
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
Factorial is in the loop but the loop does i=1,3,5 and not i=1,2,3,4,5, that's might be a problem. If the "number of terms" : "2.000000+2.666667" is two so in the loop your range avec to be until n*2 and not n but careful because the factory will be changed.

Calculating averages with inputted numbers using for loop

I'm trying to calculate an average of the numbers that are inputted following the "Enter the number" part. I don't know how to add the inputted numbers as they aren't set variables. When I run it, it says 'int object is not iterable'
I thought about asking for each number separately, but I wouldn't know how to make it repeat the set number of times for each time the question is asked. So I use the for loop, but by using the for loop, I can't set numbers to variables, so I can't add them.
Apparently sum function can help, but nothing I've looked at shows me how to use it very well.
how_many = int(input("How many numbers are there?"))
for counter in range (1, (how_many + 1)):
numbers = int(input("Enter number:"))
sum1 = sum(numbers)
av = sum1 / how_many
The result of the code is supposed to show the average of the numbers inputted but I can't figure out how to work out the total?
You can do it like this.
how_many = int(input("How many numbers are there?"))
total_sum=0
for counter in range (1, (how_many + 1)):
number = int(input("Enter number:"))
total_sum = total_sum+number
avg = total_sum / how_many
print(avg)
sum calculates the total in an iterable, eg list.
If we loop, and add the number to a list each time, we can calculate the average at the end.
This should do the trick:
how_many = int(input("How many numbers are there?"))
numbers = []
for counter in range (how_many):
numbers.append(int(input("Enter number:")))
total = sum(numbers)
av = total / how_many
print("Average:", av)
Output:
How many numbers are there?5
Enter number:1
Enter number:2
Enter number:3
Enter number:4
Enter number:5
Average: 3.0

Deleting numbers from lists.PYTHON

I am creating a python program, to allow a user to input numbers and find the total, average, highest and lowest of the numbers inputted. my teacher has told me to improve the program by removing the first and last number inputted into the list.I am confused on how doing so,as the list does not exist until the user has inputted the numbers into it.
THIS IS MY CODE SO FAR:
totalList=[]
totalList = totalList[1:-1]
TotalNum = int(input("Please enter how many numbers are to be entered:"))
Change=TotalNum
Input=0
Total=0
while TotalNum>0:
TotalNum = TotalNum - 1
Input = int(input("Enter Number: "))
totalList.insert(-1,Input)
Total = Total +Input
print("Total:" ,Total)
print("Average:",Total/Change)
print("Highest:",max(totalList))
print("Lowest:",min(totalList))

Categories

Resources