In: Computer Science
Please solve the following using if statements in python 3.
User specifies the number of books ordered, and if the order is online, or not. You can collect the second input by asking the user to enter 1 for online, and 0 for offline. You can assume that the user will enter 1 or 0 as directed.
Unit price for a book is 15$.
Online orders get shipping cost added: per book 25 cents upto 10 books, after that, flat rate of 5$.
Offline orders get taxed, at 8%.
(that is: no ship cost for offline orders, no tax for online orders)
App displays: Order price, inclusive of shipping/tax
Hints:
1. The input 1 or 0 typed by user will come in as ‘1’ or ‘0’, strings.
2. In Python, the 1 is equivalent to true, and 0 is equivalent to false. So if you have a variable holding the 1/0 value for online/ offline, you can use that variable as if it were a boolean variable. Note you can do this problem equally well without using this hint.
Code and output
Code for copying
n=int(input("Enter 1 for online 0 for offline: "))
m=int(input("Enter number of books purchased: "))
if n==1:
if m<=10:
j=(m*15)
j+=(m*25)/100
print("the total cost is {} dollars".format(j))
if m>10:
j=(m*15)
r=m-10
j+=(r*5)
j+=(10*25)/100
print("the total cost is {} dollars".format(j))
if n==0:
j=m*15
j=j+(j*(0.08))
print("the total cost is {} dollars".format(j))
Code snippet
n=int(input("Enter 1 for online 0 for offline: "))
m=int(input("Enter number of books purchased: "))
if n==1:
if m<=10:
j=(m*15)
j+=(m*25)/100
print("the total cost is {} dollars".format(j))
if m>10:
j=(m*15)
r=m-10
j+=(r*5)
j+=(10*25)/100
print("the total cost is {} dollars".format(j))
if n==0:
j=m*15
j=j+(j*(0.08))
print("the total cost is {} dollars".format(j))
Using function-
Code and output
Code for copying
def fun(n,m):
if n==1:
if m<=10:
j=(m*15)
j+=(m*25)/100
if m>10:
j=(m*15)
r=m-10
j+=(r*5)
j+=(10*25)/100
if n==0:
j=m*15
j=j+(j*(0.08))
return "the total cost is {} dollars".format(j)
n=int(input("Enter 1 for online 0 for offline: "))
m=int(input("Enter number of books purchased: "))
print(fun(n,m))
Code snippet
def fun(n,m):
if n==1:
if m<=10:
j=(m*15)
j+=(m*25)/100
if m>10:
j=(m*15)
r=m-10
j+=(r*5)
j+=(10*25)/100
if n==0:
j=m*15
j=j+(j*(0.08))
return "the total cost is {} dollars".format(j)
n=int(input("Enter 1 for online 0 for offline: "))
m=int(input("Enter number of books purchased: "))
print(fun(n,m))