Tuesday, October 30, 2018

Various IF statements

If stuff

def getNumbers():
    num1, num2=eval(input("Enter two numbers "))
    ans=divide(num1, num2)
    display(ans)

#def divide(number1, number2):
    #if number2 == 0:
       # print ("Can't divide by 0")
       # return
    #return number1/number2

def divide(number1, number2):
    if number2 != 0:
        return number1/number2
    else:
        print ("Can't divide by 0")
    return


def display(ans):
    print("the quotient is", ans)

def main():
    getNumbers()

main()
    

elif stuff

d#example of elif
from random import randrange

def getRandom():
    number=randrange(1,100)
    return number

def getGuess():
    guess=int(input ("Enter a number between 1, 100  "))
    return guess

def game():
    r = getRandom()
    for i in range(5):
        g=getGuess()
        result=evaluate(g, r)
        print(result)
        if result=="Congratulations": 
            print("the number was " + str(r))
            break



def evaluate(g, r):
    result=''
    if g > r:
        result='your guess is too high'
    elif g < r:
        result='Your guess is too low'
    else:
        result= 'Congratulations'
    return result

def main():
    game()

main()

#num=getRandom()
#print (num)

find max

myList=[3,5,24,31,6,7,53,9,14,8]

highest=myList[0]
for i in range(10):
    if myList[i] > highest:
        highest=myList[i]

print(highest)

Calculate pay

#overtime py
#calculates pay and overtime for a week

def getWage():
    wage=float(input("Enter your hourly wage: "))
    return wage

def getHours():
    hours=float(input("Enter your hours: "))
    return hours

def calculatePay():
    rate=getWage()
    hrs=getHours()
    regpay=0
    pay=0
    OT=0
    if hrs > 40:
        #pay=rate * (40 + ((hrs-40)*1.5))
        regpay=rate * hrs
        OT=rate *(hrs-40)*1.5
        pay=regpay+OT
    else:
        regpay=rate * hrs
        pay=regpay
    display(regpay, OT, pay)

def display(reg, ot, pay):
    print("Your regular pay is", reg)
    print("Your overtime pay is",ot)
    print("Your total pay is", pay)

def main():
    calculatePay()

main()

Try example

num1=float(input("Enter a number "))
num2=float(input("Enter a number "))
try:
    ans=num1/num2
    print(ans)
except:
    print ("can't divide by 0")

No comments:

Post a Comment