Format the total so it does not show decimal places - python-3.x

So, I'm trying to make this code not show any decimal places in the end (total)... But I can't really find a way to do it. Is there a way to do it without having to rewrite everything?
test1_weight = float(input("Type your 1st Test weight: "))
test2_grade = float(input("Type your 1st Test grade: "))
test2_weight = float(input("Type your 1st Test weight: "))
total = (test1_grade * test1_weight + test2_grade * test2_weight)/(test1_weight + test2_weight)
print ("The weighted average is: ", total)

You can cast total to an integer:
total = int(total)
For example:
total = 3.75
total = int(total)
print(total) # 3

Since each of your inputs are floats, your 'total' will also be a float. If you want the 'total' to not have any decimal points, you should cast total to an integer before printing: int(total)

You can also round the result with the round function
Exemple:
round(total)

You can accomplish this with f-strings. F-strings allow you to interpolate python expressions with strings, meaning you can stick your variable right in there:
>>> total = 3.75
>>> print(f"The total is: {total}")
The total is: 3.75
It also allows for formatting syntax, which means we can restrict the float to no decimal places by specifying the float format .0f after a colon.
>>> total = 3.75
>>> print(f"The total is: {total:.0f}")
The total is: 4

Related

Correct Change python program

I am supposed to
Design and write a Python program that gives exact change for an item purchased with $5 or less. Use a float data type to represent money (You may want to multiply this number by 100 so that you get an integer for your calculation, such that $5.00 represents 500 cents, $2.35 represents 235 cents, etc.)
Below is my code:
amount = float(input('Enter item cost: '))
tender = float(input('Enter amount tendered: '))
change = int((tender - amount)*100)
dollars = change // 100
change = change % 100
quarters = change // 25
change = change % 25
dimes = change // 10
change = change % 10
nickels = change // 5
change = change % 5
pennies = change
#output
print('Dollar bills:', dollars)
print('Quarters:', quarters)
print('Dimes:', dimes)
print('Nickels:', nickels)
print('Pennies:', pennies)
An expected output for an item that costs $2.71 when the amount tendered is $5.00, would be:
Dollars: 2
Quarters: 0
Dimes: 2
Nickels:1
Pennies:4
but my output is giving me:
Enter item cost: 2.71
Enter amount tendered: 5.00
Dollars: 2
Quarters: 1
Dimes: 0
Nickels: 0
Pennies: 4
What am I doing wrong? Any help would be appreciated
Thanks!

Python discount rate of 2 items plus tax program not outputting correctly

I'm writing a program that inputs 2 items, applies a 25% discount and adds a 8.25% tax rate on the total discounted items price however I keep getting a wrong output.
It works with certain numbers such as 5 + 3 but doesn't come out correctly with 8.75 + 25.99.
first_item = int(float(input("Price of 1st item: ")))
second_item = int(float(input("Price of 2nd item: ")))
discount = (first_item + second_item) *.25
discounted_items = (first_item + second_item) - discount
tax = discounted_items *.0825
final_pay = discounted_items + tax
print("You pay ", final_pay)
expected result with 8.75 + 25.99 is 28.204537 but I keep getting 26.7918.
When you convert your input from str to float it's great, but when you convert that again to an int, you lose the fractional part (.xxx), and so you lose precision, you didn't see this problem with 3 and 5 because they're already integer, and so nothing is lost, so just keep your input float:
first_item = float(input("Price of 1st item: "))
second_item = float(input("Price of 2nd item: "))

how to generate a random number with a known probability?

I would like to generate a random number between x and y with a known probability. For example, using Output=randint(0,2) i know that there is a 33% probability that Output is equal to 1.
Similarly, if Output=randint(0,3) i know that there is 25% probability that the Output is equal to 1.
How can i generate a random number similar to above to make sure that there is 40% probability that Output equal to 1?
Thanks a lot
Not sure what's your aim here, how about getting 40% by giving a fixed range, and than manipulating the results?
For example, this will give you 40% value of 1, and 30% of 2 or 3
num = randint(0,9)
if num <= 3:
num = 1
elif num <= 6:
num = 2
else:
num = 3
Here I have an alternative solution, looks shorter and since there are no if statements it should be much faster.
Also, you can easily change what number is to be returned with the 60% probability
a = [1,1,0,0,0]
num = a[randint(0,4)]
As alternative here is a version of the same with a single line:
num = list((1,1,0,0,0))[randint(0,4)]

For loop multiply in some range

I have a task for my college in which I must program some very simple logic for multiplying numbers in range like 1 to 5 and than multiply like 1*2*3*4*5. And this way for any input number. For 7 it would be 1*2*3*4*5*6*7.
This is my modest code which is not finished because of no idea how to do it .Help, please..
number = int(input("Enter a number:"))
number += 1
for i in range(1,number):
a = i*(number*
print(a)
Try this:
In [1774]: number = int(input("Enter a number:"))
In [1775]: a = 1
In [1776]: for i in range(1, number+1):
...: a *= i
In [1781]: a
Out[1781]: 120
a's value is 120 which is basically (1*2*3*4*5). Hope this helps.

Python - Negative numbers not adding, but positive numbers do

I am supposed to add up the rows and the grand total of all the numbers. I can add the grand total well, but I am unable to add the row that has negative numbers only. The following code adds up the positive numbers but do not add up the negative numbers correctly.
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + x)
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
When the user input is:
1,1,-2 -1,-2,-3 1,1,1
My output is: 0
1
3
-3
instead of: 0
-6
3
-3
I know it has something to do with the first for loop, but I can't figure it out. When I try this:
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + (numbers[x]))
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
I get the error message:
File "list.py", line 14, in
sumRow = (sumRow + (numbers[x]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Why doesn't my code add up the negative numbers? Any help is greatly appreciated!
Where you say
sumRow = (sumRow + (numbers[x]))
To add integers you say 1+1, not (1+(1)) this would be adding to lists so you could change that.
From my understanding numbers is an array as well, so saying
numbers[x]
Will give you many numbers. What you want is the total for every row, and the total of all rows. Heres a program that does this. I am assuming that your program automatically gets numbers from the user input.
grandTotal = 0
for row in numbers:
#for each row we find total amount
rowTotl = 0
for value in row:
#for each column/ value we add tot the row total
rowTotl += value
print(rowTotl)
#add this row's value to the grandTotal before we move on
grandTotal += rowTotl
#After all adding we print grand total
print(grandTotal)
The reason your program doesn't add negative numbers, is really because the row totals are not adding numbers at all. They are just adding the indexes, rather than the value, so they don't work for positive number either. The grand total works because you are adding all the values properly, rather than adding the indexes. FYI,
for index in range(len(numbers)) :
does not give you the values, but rather : 0,1,2,3,4,5,6...(indexes) till the end of the range, to get the value numbers you would do
for value in numbers:

Resources