This is my code (Python 3.2)
Total = eval(input("How many numbers do you want to enter? "))
#say the user enters 3
for i in range(Total):
Numbers = input("Please enter a number ")
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered is", Numbers)
#It displays 3 instead of 6
How do i get it to add up properly?
Just a quick and dirty rewrite of your lines:
Total = int(input("How many numbers do you want to enter? "))
#say the user enters 3
Numbers=[]
for i in range(Total):
Numbers.append(int(input("Please enter a number "))
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered is", sum(Numbers))
#It displays 3 instead of 6
I assume that you use Python 3 because of the way you print, but if you use Python 2 use raw_input instead of input.
This code will fix your problem:
total = int(input("How many numbers do you want to enter? "))
#say the user enters 3
sum_input = 0
for i in range(total):
sum_input += int(input("Please enter a number "))
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered are", sum_input)
A number of comments:
You should stick to pep8 for styling and variable names. Specifically, use under_store for variable names and function names, and CapWords for class names.
The use of eval is questionable here. This article explains very well on why you shouldn't use eval in most cases.
You need to declare your variable outside the for loop, and keep on adding input numbers to it in the loop..
numbers = 0
for i in range(Total):
numbers += int(input("Please enter a number "))
print ("The sum of the numbers you entered are", numbers)
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 14 days ago.
i'm new in python
and for practice i make this codes but i have a problem :when you choose a number more than 100 it says you entered a wrong number and you must enter another number then if you enter a right number you wont get the awnser
this is outout
please enter a number between 0 and 100 =>123
you have entered a number more than 100 or less than 0 !
so please enter a number between 0 and 100 =>12
and nothing !!!
but if you enter a wrong number for two times or more it will work perfectly
this is my code
print("welcome to or simple test")
def number_choosing_1():
number_1=int(input("please enter a number between 0 and 100 "))
if 0<number_1 and number_1<100 and number_1%2==0:
print("the number you have entered is even ")
elif 0<number_1 and number_1<100 and number_1%2==1:
print("you have entered a odd number ")
else :
if number_1>100 or number_1<0:
wrong_number_choosing_1()
elif 0<number_1 and number_1<100:
number_choosing_1()
def number_choosing_2():
number_1=int(input("that's it now fore make me sure reenter your number "))
if 0<number_1 and number_1<100 and number_1%2==0:
print("the number you have entered is even ")
elif 0<number_1 and number_1<100 and number_1%2==1:
print("you have entered a odd number ")
else :
if number_1>100 or number_1<0:
wrong_number_choosing_1()
elif 0<number_1 and number_1<100:
number_choosing_1()
def wrong_number_choosing_1():
number_1=int(input("""you have entered a number more than 100 or less than 0 !
so please enter a number between 0 and 100 """))
while number_1>100 or number_1<0:
number_1=int(input(" come on again !! please enter a number between 0 and 100 "))
if 0<number_1 and number_1<100:
number_choosing_2()
number_choosing_1()
any help appreciated .
just use a loop to ask for an input until a valid answer is given, then break the loop
while True:
number = int(input("Enter a number between 0 and 100: "))
if 0 <= number <= 100:
break
else:
print("Wrong number, try again")
# then check if the number is even or odd
parity = "odd" if number % 2 else "even"
print(f"The number {number} is {parity}")
I have only been learning Python for about two weeks and need a bit of help with an assignment. Below is the code I have written so far,
import random
colour_list = ["red","blue","white","yellow","pink","orange","black","green","grey","purple"]
start_num = int(input("enter a starting number between 0 and 4\n"))
end_num = int(input("enter a end number between 5 and 9\n"))
print ("You chose ",random.choice(colour_list))
The task is as follows "Ask the user for a starting number between 0 and 4 and an end number between 5 and 9. Display the list for those colours between that start and end numbers the user input"
Can anyone suggest how I would link the user input to the list? Thanks in advance
You don't need to use random here. You can do simple list slicing,
colour_list = ["red","blue","white","yellow","pink","orange","black","green","grey","purple"]
start_num = int(input("enter a starting number between 0 and 4\n"))
end_num = int(input("enter a end number between 5 and 9\n"))
print("You chose ",colour_list[start_num:end_num])
I need to modify my program so it includes a prime read with a loop. the document is saying for my getNumber function it should ask the user only to input a number between 2 and 30, the getScores function should ask the user to input a number between 0 and 100. it they don't get a number between that it should tell them re enter a number. I don't get any errors when running the program but not sure what I am missing in order to make sure its running properly to include the re enter a number part. here is the code:
# main
def main():
endProgram = 'no'
print
while endProgram == 'no':
totalScores = 0
averageScores = 0
number = 0
number = getNumber(number)
totalScores = getScores(totalScores, number)
averageScores = getAverage(totalScores, averageScores, number)
printAverage(averageScores)
endProgram = input('Do you want to end the program? yes or no ')
while not (endProgram != 'yes' or endProgram != 'no'):
print('Enter yes or no ')
endProgram = input('Do you want to end the program? (yes or no )')
# this function will determine how many students took the test
def getNumber(number):
number = int(input('How many students took the test: '))
return number
while number < 2 or number > 30:
print('Please enter a number between 2 and 30')
number = int(input('How many students took the test: '))
# this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = int(input('Enter their score: '))
return totalScores
while score < 0 or score > 100:
print('Please enter a number between 0 and 100')
score = int(input('Enter their score: '))
return score
# this function will calculate the average
def getAverage(totalScores, averageScores, number):
averageScores = totalScores / number
return averageScores
# this function will display the average
def printAverage(averageScores):
print ('The average test score is: ', averageScores)
# calls main
main()
First suggestion is to change this:
number = int(input('How many students took the test: '))
reason is that, as it is written, this takes the user input and implicitly assumes that it can be cast to an int. What happens if the user enters "hello, world!" as input? It is necessary to take the user input first as a string, and check if it would be valid to convert it:
number = input("enter a number:")
if number.isdecimal():
number = int(number)
Next, the function as a whole has some structural problems:
def getNumber(number):
number = int(input('How many students took the test: '))
return number
while number < 2 or number > 30:
print('Please enter a number between 2 and 30')
number = int(input('How many students took the test: '))
number is passed in as an argument to getNumber. Then the name number is reassigned to the result of reading the user input, and returned... Make sure you understand what the return statement does: once the control flow reaches a return statement, that function terminates, and it sends that value back to the caller. So your while loop never runs.
Maybe this would work better:
def getNumber():
while number := input("enter a number"):
if number.isdecimal() and int(number) in range(0, 31):
return int(number)
print('Please enter a number between 2 and 30')
Why isn't my code not looping with the Y/N condition, correctly?
Write a Python program to do the following:
(a) Ask the user to enter as many integers from 1 to 10 as he/she wants. Store the integers entered by the user in a list. Every time after the user has entered an integer, use a yes/no type question to ask whether he/she wants to enter another one.
(b) Display the list.
(c) Calculate and display the average of the integers in the list.
(d) If the average is higher than 7, subtract 1 from every number in the list. Display the modified list.
person = []
integer_pushed = float(input("Enter as many integers from 1 to 10"))
person.append(integer_pushed)
again = input("Enter another integer? [y/n]")
while integer_pushed < 0 or integer_pushed > 10:
print('You must type in an integer between 0 and 10')
integer_pushed = float(input("Enter as many integers from 1 to 10"))
person.append(integer_pushed)
again = input("Enter another integer? [y/n]")
while again == "y":
integer_pushed = float(input("Enter as many integers from 1 to 10"))
person.append(integer_pushed)
again = input("Enter another integer? [y/n]")
If you're using Python 2.7 input() attempts to evaluates the input as a Python expression. You want to use raw_input() instead.
In Python3, input() has the desired behavior.
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))