def main():
totalprofit = 0
stockname = input("Enter the name of the stock or -999 to quit: ")
while stockname != "-999":
sharesbought, purchasingprice, sellingprice, brokercommission = load()
amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss = calc(sharesbought, purchasingprice, sellingprice, brokercommission)
output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofpaidcommission, profitorloss)
stockname = input("Enter the name of the next stock (or -999 to quit): ")
totalprofit += profitorloss
print("\n Total profit is: ", format(totalprofit, '.2f'))
def load():
sharesbought = int(input("Number of shares bought: "))
purchasingprice = float(input("Purchasing price: "))
sellingprice = float(input("Selling price: "))
brokercommission = float(input("Broker commission: "))
return sharesbought, purchasingprice, sellingprice, brokercommission
def calc(sharesbought, purchasingprice, sellingprice, brokercommission):
amountpaid = sharesbought * purchasingprice
amountofpaidcommission = amountpaid * (brokercommission/100)
amountstocksoldfor = sharesbought * sellingprice
amountofsoldcommission = amountstocksoldfor * (brokercommission/100)
profitorloss = (amountpaid + amountofpaidcommission) - (amountstocksoldfor - amountofsoldcommission)
return amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss
def output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss,):
print("\n Stock name: ", stockname, sep = '')
print("Amount paid for the stock: ", format(amountpaid, '.2f'))
print("Commission paid to broker when the stock was bought: ", format(amountofpaidcommission, '.2f'))
print("Amount the stock sold for: ", format(amountstocksoldfor, '.2f'))
print("Commission paid to broker when the stock was sold: ", format(amountofsoldcommission, '.2f'))
print("Profit or loss: ", format(profitorloss, '.2f'))
main ()
The objective of the first function is to allow a user to input the followings as many times as she or he wants until the user decides is done:
Stock name
Shares bought
Selling price
Broker commission
My main problem is then in the main function. I am skeptical whether I am using the while loop correctly or if it's correct at all. I tried to run the program but it won't output anything.
Also, shouldn't I add this at the end of the program with the values inputted to call all the functions above:
def main()
load()
calc()
output()
Or is it fine within the while loop?
I think a while loop is perfectly appropriate for this use case, where you want to loop an indeterminate number of times, stopping when some condition is not met.
There is one obvious problem, on this line:
stockname +=1
This doesn't make any sense. Since stockname is a string, you can't add one to it. Instead, you should be asking the user for the next stock name (or a "special" value to signal they're done). Try replacing that line with something like:
stockname = input("Enter the name of the next stock (or -999 to quit): ")
The rest of your code appears correct, if rather verbose. Unless you think it's likely you'll call some of your other functions in some other place in your code, it might be simpler and cleaner to include all the logic in one function. Functions are nice, but you should balance the benefits of isolating each part of your code in its own function against the effort of passing lots of values between them.
Related
I cannot get Pytest to test anything involving this code. It always says no tests ran when I use the code below. I'm trying to just get it to test and from there I can tinker with the rest, I'm just a bit stuck on this and can't figure it out. The code runs well, exactly as intended it's just when I try to pytest with it that I'm reciving a problem.
import datetime
tday = datetime.date.today()
balance = 1000.0
amount = float
transaction_list = []
class Account(amount):
def get_balance(self):
print("\n This is your account balance: " + str(balance))
def deposit(self, amount):
global balance
amount = float(input("\n How much would you like to deposit? "))
if amount > 10000:
print("\n Do you want to swim in the vualt? ")
transaction_list.append("You deposited " + str(amount))
balance = balance + amount
print("\n You're balance is now " + str(balance))
def withdrawl(self, amount):
global balance
amount = float(input("\n How much do you wanna take out? "))
while amount > balance:
amount = float(input("\n You don't have enough funds. Enter a new value "))
transaction_list.append("\n You withdrew " + str(amount))
balance = balance - amount
print("\n You're balance is now " + str(balance))
class Transaction(amount):
def transaction(self):
print("\n")
print(" " + str(card_number) + " " + str(tday))
print("\n Recipt: HM21-1926\n")
print("\n Account: Chequing. Primary\n")
print("\n Here is your transaction history!\n")
for i in transaction_list:
print(i)
print("\n Account Balance: " + str(balance))
print("\n Limited time offer: get a personal loan starting at Prime + 1.99%*")
print("\n Offer valid until September 2, 2022.")
print("\n Conditions apply & approval required. Ask us for details.\n")
account = Account()
transaction = Transaction()
if __name__ == "__main__":
mike_lane = Account()
print(repr(mike_lane))
print(str(mike_lane))
The code below is the code I'm trying to use pytest on. I don't know if there is something wrong with what I'm trying to test or if there is something wrong with the main code. I'm just starting out with programming and this is a bit complicated for me still (However, I have to learn to do this for the class so I'm not able to come back to learn TDD later on)
import bank_program
def test_deposit():
account = Account(100)
assert balance.amount == 1100
def test_depos():
with pytest.raises(ValueError):
assert amount = Account("ABC")
def test_transactions():
account = Account(100)
assert tday() >= now
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I have been asked to make conversion system, the conversion doesn't have to be correct. I need help with only this following task.
Task 2:
The function get_amount_to_convert() needs to be completed.
• It should ask the user for an amount in GBP they would like to convert
• If the amount is not a numerical value it should output that a numerical value is expected and
ask for a correct value to be input.
• Otherwise it should return float(amountToConvert)
Test your work by running the script. If it works then the value that is entered should be displayed in the
text at the bottom of the screen.
This is currently all i need to do so there's no need to help with the rest.
'''
travelagent.py
##########################################
#### Travel Agency ####
##########################################
Bureau de Change system to convert from GBP
to requested currency.
Suported currencies: GBP,USD,EUR,BRL,JPY,TRY
Version 0.01
Author:
email:
'''
#set up some dictionaries to store data - leave alone
exchangeRate = {
#"GBP":0,
"USD":1.40,
"EUR":1.14,
"BRL":4.77,
"JPY":151.05,
"TRY":5.68
}
#set up some variables for general use
baseTransactionFee = 0.35 #percentage transaction fee for upto 300GBP
staffDiscountRate = 0.05 #percentage discount rate to apply to staff discounts
staffMember = False #Initial status of staff member discount
transactionFee = 0 #Initial status of transactionFee
def welcome_message(): #get amount to convert
print("Hello\nWelcome to the Bureau De Change System")
def get_amount_to_convert():
amountToConvert = input("How much would you like to convert from GPB? ")
if amountToConvert == str:
print("That is not a valid amount, please input a number")
get_amount_to_convert
elif amountToConvert != str:
return float(amountToConvert)
get_amount_to_convert()
def get_currency_to_convert_to():
currencyToConvertTo = "usd"
return currencyToConvertTo
def is_staff_member(): #find out if this is for a staff member
return True
def convert_currency(currencyToConvertTo,amountToConvert):
convertedAmount = 0
return convertedAmount
def work_out_the_fee(transactionFee,amountToConvert):#Work out the fee
if amountToConvert <= 5:
transactionFee = transactionFee+(amountToConvert*0.035)
return transactionFee
def work_out_total_cost(transactionFee,amountToConvert): #workout the total cost
totalCost = 0
return totalCost
# Leave the code below alone **NO NEED TO EDIT IT** :)
def print_result(amountToConvert,currencyToConvertTo,convertedAmount,transactionFee,totalCost,staffMember):
print("\nThe conversion is as follows:\n")
print("Converting %s GPB to %s results in an amount of %s %s" %(str(format(amountToConvert,'.2f')),currencyToConvertTo,str(format(convertedAmount,'.2f')),currencyToConvertTo,))
print("The transaction fee for this is %s GBP" % (str(format(transactionFee,'.2f')),))
if staffMember == True:
discountAmount = transactionFee * staffDiscountRate
print("A staff discount of %s GBP will also be applied" %(str(format(discountAmount,'.2f'))))
totalCost = totalCost - discountAmount
print("\nThis results in a total charge of %s GBP" % format(totalCost,'.2f'))
welcome_message()
amountToConvert = get_amount_to_convert()
targetCurrency = get_currency_to_convert_to()
staffMember = is_staff_member()
convertedAmount =convert_currency(targetCurrency,amountToConvert)
transactionFee = work_out_the_fee(transactionFee,amountToConvert)
totalCost = work_out_total_cost(transactionFee,amountToConvert)
print_result(amountToConvert,targetCurrency,convertedAmount,transactionFee,totalCost,staffMember)
something like this should help:
def get_amount_to_convert():
amountToConvert = input("How much would you like to convert from GPB? ")
try:
return float(amountToConvert)
except:
print("this is not a valid value, please enter a numeric value")
get_amount_to_convert()
get_amount_to_convert()
The variable amountToConvert is not exactly the same as the built-in str. Try using a try/except.
def get_amount_to_convert():
try:
return float(input("How much would you like to convert from GPB? "))
except ValueError:
print("That is not a valid amount, please input a number")
return get_amount_to_convert()
I'm resolving a basic problem consisting of make a list of products, the user choose the product and the amount of the product and the total price is printed. I get a keyerror in the line 22.
def main():
print("Choose a product: ")
print("")
print("Products: ")
print("")
print("Samsung Galaxy S10+.............1")
print("Samsung Galaxy S10..............2")
print("OnePlus 7 Pro...................3")
print("OnePlus 7.......................4")
print("OnePlus 6t......................5")
print("Huawei P30 Pro..................6")
print("Huawei Mate 20 Pro..............7")
print("Google Pixel 3XL................8")
print("Gooogle Pixel 3A XL.............9")
print("Oppo Reno 10x Zooom............10")
print("")
relation = {1:1000, 2:900, 3:700, 4:600, 5:470, 6:850, 7:970, 8:950, 9:300, 10:550}
code = input("Enter the product code: ")
print("")
print("The price is $", relation[code])
quantify = input("Enter amount: ")
print("")
totalPrice = float(relation[code] * quantify)
print("The total price is: $", totalPrice)
The error displayed is
Traceback (most recent call last):
File "main.py", line 30, in <module>
main()
File "main.py", line 22, in main
print("The price is $", relation[code])
KeyError: '5'
In this case I choose the product code "5".
When you use input it returns a string, not an integer. You can see this because the error message shows '5', not 5. The keys to your dictionary are integers, though, so the key you are providing in the statement (code) is not found.
You could instead use
print("The price is $", relation[int(code)])
A better format, at least in Python 3.6 and later, would be
print(f"The price is ${relation[int(code)]}")
for line 26, the problem is similar. Just convert to integers (or float, if there's a decimal point)
totalPrice = float(relation[int(code)] * int(quantify))
or
totalPrice = relation[int(code)] * float(quantify)
input in python receives data as a string, you need to typecast it
it is something along:
print("The price is $", relation[int(code)])
I think you should also follow the Python idiom EAFP (Easier to ask for forgiveness than permission) here when asking for user input as he could write literally everything but integers you expect:
while True:
code = input("Enter the product code: ")
try:
price = relation[int(code)]
except (ValueError, KeyError):
print("Error: Incorrect code value, try again!")
else:
break
def main():
print("Choose a product: ")
print("")
print("Products: ")
print("")
print("Samsung Galaxy S10+.............1")
print("Samsung Galaxy S10..............2")
print("OnePlus 7 Pro...................3")
print("OnePlus 7.......................4")
print("OnePlus 6t......................5")
print("Huawei P30 Pro..................6")
print("Huawei Mate 20 Pro..............7")
print("Google Pixel 3XL................8")
print("Gooogle Pixel 3A XL.............9")
print("Oppo Reno 10x Zooom............10")
print("")
relation = {1:1000, 2:900, 3:700, 4:600, 5:470, 6:850, 7:970, 8:950, 9:300, 10:550}
code = input("Enter the product code: ")
print("")
print("The price is $", relation[code])
quantify = input("Enter amount: ")
print("")
totalPrice = float(relation[int(code)] * quantify)
print("The total price is: $", totalPrice)
you need to take the input as integer because the input() take the default as a string so you can type it like quantify = int(input("Enter amount: "))
or another method is to use int() in the place where the calculations are like
totalPrice = float(relation[int(code)] * int(quantify))
def main():
def load():
name=0
count=0
totalpr=0
name=input("Enter stock name OR -999 to Quit: ")
while name != '-999':
count=count+1
shares=int(input("Enter number of shares: "))
pp=float(input("Enter purchase price: "))
sp=float(input("Enter selling price: "))
commission=float(input("Enter commission: "))
name = input("Enter stock name OR -999 to Quit: ")
def calc():
amount_paid=shares*pp
commission_paid_purchase=amount_paid*commission
amount_sold=shares*sp
commission_paid_sale=amount_sold*commission
profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
totalpr=totalpr+profit_loss
def print():
print("\nStock Name:", name)
print("Amount paid for the stock: $", format(amount_paid, '10,.2f'))
print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
print("Amount the stock sold for: $", format(amount_sold, '10,.2f'))
print("Commission paid on the sale: $", format(commission_paid_sale, '10,.2f'))
print("Profit (or loss if negative): $", format(profit_loss, '10,.2f'))
print("Total Profit is $", format(totalpr, '10,.2f'))
return main()
load() #to input the values
calc()
print()
I'm not sure what I'm doing wrong:
Should I be putting in variable names into def load():, def calc(): and def print():?
As I run it now, it says "load" is not defined. How would I go about defining load? And for that matter, if I didn't define def calc(): and def print(): how do I define those?
I do properly call them at the end of the code, in the order that I'd like to call them -- load, calc, and then print.
Is my "return main()" the right thing to do? I don't know, I Just want this code to run properly without error that's all. I'm under the impression that I'm just missing a few things. Any help would be appreciated.
You are defining load() inside the scope of main(). This means you cannot use the function outside of main().
The easy solution is that you should put your function defines for load, calc, and print outside of the definition of main() (btw, call it something else like print_stock info, print is already a function!)
You also do not need to return main(). I am unsure what you are trying to do, but it is not necessary at all.
yikes.
your first mistake: calling a function in another function. Maybe you meant to do
class Main:
def load(self):
#do a thing
then you'd have to do
main = Main()
main.load()
your second mistake was defining a new a print() function that uses print functions, as one already exists. Rename it, as that will cause a huge error
This is a minimal working example to illustrate how one can solve what you are trying to do.
# helper for interpreting input
import ast
#: initial starting values
thing, count, cost, total = 'Nothing', 0, 0, 0
# define all functions at module indentation
def get_user_input():
global thing, count, cost # write to the GLOBAL names thing, count and cost
thing = input('What have you?') # ask for a string
count = ast.literal_eval(input('How much do you have?')) # ask for a string and interpret it: "3.2" => float
cost = ast.literal_eval(input('How much does each cost?'))
def compute():
global total # write to global name total
total = count * cost # can read from global names
def pretty_print(): # not named print! we still need print to print!
print("You have", count, thing)
print("At", cost, "each, that makes", total, "in total")
# call functions at module indentation
get_user_input() # ducks, 3, 42
compute()
pretty_print()
# You have 3 ducks
# At 42 each, that makes 126 in total
One thing to warn you about: working with global variables is generally a bad idea. For a small script it's fine, but you've already messed up the basics - globals are tricky, so avoid them whenever possible. If you just want to run these commands, do not write them as functions, execute them directly. Basically, leave away the def ... and global ... lines and put everything on the same indentation, then remove the last three lines.
If you really want to store and print multiple things, you need to use containers. Just assigning to a value in a loop, e.g. thing = input('What have you?'), will leave it at the last value entered. Instead, you need a container like a list. You can then append additional values to it.
container = [] # [] is a list literal
for _ in range(3): # non-infinite loop
next_thing = ast.literal_eval(input('Place your order...\n'))
container.append(next_thing)
print("You ordered", container)
def load():
global name
global count
global shares
global pp
global sp
global commission
name=input("Enter stock name OR -999 to Quit: ")
count =0
while name != '-999':
count=count+1
shares=int(input("Enter number of shares: "))
pp=float(input("Enter purchase price: "))
sp=float(input("Enter selling price: "))
commission=float(input("Enter commission: "))
calc()
display()
name=input("\nEnter stock name OR -999 to Quit: ")
def calc():
global amount_paid
global amount_sold
global profit_loss
global commission_paid_sale
global commission_paid_purchase
global totalpr
totalpr=0
amount_paid=shares*pp
commission_paid_purchase=amount_paid*commission
amount_sold=shares*sp
commission_paid_sale=amount_sold*commission
profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
totalpr=totalpr+profit_loss
def display():
print("\nStock Name:", name)
print("Amount paid for the stock: $", format(amount_paid, '10,.2f'))
print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
print("Amount the stock sold for: $", format(amount_sold, '10,.2f'))
print("Commission paid on the sale: $", format(commission_paid_sale, '10,.2f'))
print("Profit (or loss if negative): $", format(profit_loss, '10,.2f'))
def main():
load()
main()
print("\nTotal Profit is $", format(totalpr, '10,.2f'))
I've been working on a project for my class in python and I feel like no matter what I do I can't get it right. We're supposed to create a program that captures 3 items and then 4 bits of information for each item. AKA: Item: x, Quantity: x, price $x, weight x pounds. Then it should print out a subtotal, shipping & handling cost, tax total, and final total. The issue I'm having is that we're not allowed to create a variable for each item and must do everything in a loop. No matter what I've tried all I can do is get everything to print out the last data entered.
Here's my code so far:
def main():
#Range of 3 so that user may enter 3 seperate items
for i in range(3):
#Grabs user input for item description and/or name
strDescription = str(input("Enter description of item: "))
#Grabs user input for price of item
fltPrice = float(input("Enter price of item: $ "))
#Grabs user input for quanitity of item
intQuantity = int(input("Enter quantity of item: "))
#Grabs user input for weight of item in pounds
fltWeight = float(input("Enter weight of item in pounds: "))
#Subtotal is equal to price times number of items purchased
fltPriceSub = fltPrice*intQuantity
#Shipping 25 cents per pound
intShipping = (.25*fltWeight)
#There is a base fee of $5 per order applied to shipping
intBaseRate = 5
#Tax is 9 cents per dollar
fltTax = 0.09*fltPriceSub
fltPriceSubTotal = fltPriceSub+fltPriceSub+fltPriceSub
intShippingTotal = intShipping+intShipping+intShipping+intBaseRate
fltTaxTotal = fltTax+fltTax+fltTax
#Order total is the subtotal+shipping+tax added together
fltOrderTotal = fltPriceSubTotal+intShippingTotal+fltTaxTotal
print("You have purchased ", strDescription, "")
print("Your subtotal is ", fltPriceSubTotal, "")
print("Shipping and handling is ", intShippingTotal, "")
print("The tax is ", fltTaxTotal, "")
print("The order total is ",fltOrderTotal, "")
main()
If anyone could steer me in the right direction I would really appreciate it.