I am trying to write a program which asks an input of two numbers and then prints the sum, product and average by running it. I wrote a program but it asks an input for 2 numbers everytime i need a sum or average or product. How can I get all 3 at once, just making two inputs once.
sum = int(input("Please enter the first Value: ")) + \
int(input("Please enter another number: "))
print("sum = {}".format(sum))
product = int(input("Please enter the first Value: ")) * \
int(input("Please enter another number: "))
print ("product = {}".format(product))
Use variables to store the input:
first_number = int(input("Please enter the first Value: "))
second_number = int(input("Please enter another number: "))
sum = first_number + second_number
product = first_number * second_number
average = (first_number + second_number) / 2
print('Sum is {}'.format(sum))
print('product is {}'.format(product))
print('average is {}'.format(average))
You need to assign your numbers to variables and then reuse them for the operations.
Example
x = int(input("Please enter the first Value: "))
y = int(input("Please enter another number: "))
print("sum = {}".format(x+y))
print("product = {}".format(x*y))
print("average = {}".format((x+y)/2))
You're going to want to get the numbers first, then do your operation on them. Otherwise you're relying on the user to alway input the same two numbers:
a = int(input("Please enter the first Value: "))
b = int(input("Please enter the second Value: "))
print ("sum = {}".format(a+b))
print ("product = {}".format(a*b))
print ("average = {}".format((a*b)/2))
Related
I was having a small issue while I was trying to write a program to calculate simple interest in Python.
def si(p,r=100,t=2):
return (p*r*t)/100
x=float(input("Enter principal amount"))
y=float(input("Enter rate"))
z=float(input("Enter time"))
print (si(x,y,z))
I want to make y and z optional. Any idea how can I do? If I leave it blank and press enter it shows error.
The easiest way is using or
def si(p,r,t):
return (prt)/100
x = float(input("Enter principal amount: "))
y = float(input("Enter rate: ") or 100)
z = float(input("Enter time: ") or 2)
print (si(x,y,z))
Enter principal amount: 1
Enter rate:
Enter time:
2.0
But correct way is validating input before converting it to float
def si(p,r=100,t=2):
return (p*r*t)/100
x = input("Enter principal amount: ")
y = input("Enter rate: ")
z = input("Enter time: ")
def validate(param):
return float(param) if param else 1
print (si(validate(x), validate(y), validate(z)))
Output:
Enter principal amount: 1
Enter rate:
Enter time:
0.01
I type in seven digits to be able to work out a gtin-8 product code. But if I type in more than seven digits, the if len statement is meant to recognise that I have typed in more than seven digits, but it doesn't. I tried putting this into an variable but that did not work either... Any help would be appreciated!!! This is my code........
gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))
gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7)
if len(gtin_join) == 7:
What you probably want to do is something like that (notice that I'm using a list here):
ls = []
while len(ls) < 7:
try: #always check the input
num = int(input("Enter your {0} digit:".format(len(ls)+1) ))
ls.append(num)
except:
print("Input couldn't be converted!")
print(ls) #ls now has 7 elements
Your created tuple always has a length of 7 so your if-statement always turns out to be True.
For the difference between list and tuple please see this question here.
Your gtin_join is tuple, and if you want list you should use square braces. You can test type of variables with this example:
gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))
gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7)
print(type(gtin_join))
gtin_join = [gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7]
print(type(gtin_join))
if len(gtin_join) == 7:
print 7
I would do the following:
gtin_list = []
while len(gtin_list) != 7:
gtin = input("Please enter all 7 digits separated by commas...")
gtin_list = [int(x) for x in gtin.split(",")]
Question: Create a program that allows the user to enter 10 different integers. If the user tries to enter
an integer that has already been entered, the program will alert the user immediately and
prompt the user to enter another integer. When 10 different integers have been entered,
the average of these 10 integers is displayed.
This is my code:
mylist = []
number = int(input("Enter value: "))
mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
if number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
else:
mylist.append(number)
print(sum(mylist)/float(len(mylist)))
This kind of works but I need to create a loop that will keep on asking the user for another number if the number is in the array. Can you help?
What about:
mylist = []
number = int(input("Enter value: ")) mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
while number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
print(sum(mylist)/float(len(mylist)))
When I run the code below, I get the following error: eval() arg 1 must be a string or code object
Anyone know why? This is code i'm learning from a book, so I assumed that it would be correct.
# Prompt the user to enter three numbers
number1 = eval(input("Enter the first number: "))
number2 = eval(input("Enter the second number: "))
number3 = eval(input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print("The average of", number1, number2, number3, "is", average)
You are using input() on Python 2, which already runs eval() on the input. Just remove the eval() call, or replace input() with raw_input().
Alternatively, use Python 3 to run this code, it is clearly aimed at that version. If your book uses this syntax, then you want to use the right version to run the code samples.
Most of all, don't use input() on Python 2 or eval() on Python 3. If you want integer numbers, use int() instead.
Python 2 example:
# Prompt the user to enter three numbers
number1 = int(raw_input("Enter the first number: "))
number2 = int(raw_input("Enter the second number: "))
number3 = int(raw_input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print "The average of", number1, number2, number3, "is", average
Python 3 version:
# Prompt the user to enter three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print("The average of", number1, number2, number3, "is", average)
I'm pretty new to Python, and what makes me mad about my problem is that I feel like it's really simple.I keep getting an error in line 8. I just want this program to take the numbers the user entered and print the largest and smallest, and I want it to cancel the loop if they enter negative 1.
'int' object is not iterable is the error.
print "Welcome to The Number Input Program."
number = int(raw_input("Please enter a number: "))
while (number != int(-1)):
number = int(raw_input("Please enter a number: "))
high = max(number)
low = min(number)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
The problem is that number is an int. max and min both require lists (or other iterable things) - so instead, you have to add number to a list like so:
number = int(raw_input("Please enter a number: "))
num_list = []
while (number != int(-1)):
num_list.append(number)
number = int(raw_input("Please enter a number: "))
high = max(num_list)
low = min(num_list)
Just as a note after reading dr jimbob's answer - my answer assumes that you don't want to account for -1 when finding high and low.
That's cause each time you pass one integer argument to max and min and python doesn't know what to do with it.
Ether pass at least two arguments:
least_number = min(number1, number2,...,numbern)
or an iterable:
least_number = min([number1, number2, ...,numbern])
Here's the doc
You need to change number to an list of numbers. E.g.,
print "Welcome to The Number Input Program."
numbers = []
number = int(raw_input("Please enter a number: "))
while (number != -1):
numbers.append(number)
number = int(raw_input("Please enter a number: "))
high = max(numbers)
low = min(numbers)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
As mentioned by another answer, min and max can also take multiple arguments. To omit the list, you can
print "Welcome to The Number Input Program."
number = int(raw_input("Please enter a number: "))
high = low = number
while (number != int(-1)):
number = int(raw_input("Please enter a number: "))
high = max(high, number)
low = min(low, number)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
num = ''
active = True
largest = 0
smallest = 0
while active:
num = input("Please enter a number")
if num == 'done':
active = False
break
else:
try:
num = int(num)
if largest < num:
largest = num
if smallest == 0 or smallest > num:
smallest = num
except ValueError:
print("Please enter a valid number")
print("Largest no. is " + str(largest))
print("Smallest no. is " + str(smallest))