การใช้ if elif while python ค่ะ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การใช้ if elif while python ค่ะ

Re: การใช้ if elif while python ค่ะ

โดย mindphp » 05/11/2019 10:21 am

มีสมาชิกมาแนะนำให้แล้วในนี้
viewtopic.php?f=16&t=61708

การใช้ if elif while python ค่ะ

โดย Mind Mild » 03/11/2019 8:20 am

โค้ด: เลือกทั้งหมด

print("Welcome to Ticketing System")
name = (input("Enter your name : "))

height = (input("Enter your height (cm) : "))

age = int(input("Enter your age : "))

if age == 0:
    print("Please enter your age again")
    age = int(input("Enter your age : "))
if age >= 100:
    print("Please enter your age again")
    age = int(input("Enter your age : "))
if age <= 12:
    print("Your ticket price : 60 Baht")
elif age >= 13:
    print("Your ticket price : 120 Baht")

pay = int(input("Payment amount : "))

if pay < 60:
    print("Please enter your payment again")
    cost = int(input("Payment amount : "))

change = int(pay) - 60
print("Your change : " + str(change) + " Baht")

while (change > 0):
    if change // 1000 >= 1:
        print("Change 1000 Baht : " + str(change // 1000),"Cash")
        change %= 1000
    elif change // 500 >= 1:
        print("Change 500 Baht : " + str(change // 500),"Cash")
        change %= 500
    elif change // 100 >= 1:
        print("Change 100 Baht : " + str(change // 100),"Cash")
        change %= 100
    elif change // 50 >= 1:
        print("Change 50 Baht : " + str(change // 50),"Cash")
        change %= 50
    elif change // 20 >= 1:
        print("Change 20 Baht : " + str(change // 20),"Cash")
        change %= 20
    elif change // 10 >= 1:
        print("Change 10 Baht : " + str(change // 10),"Coin")
        change %= 10
    elif change // 5 >= 1:
        print("Change 5 Baht : " + str(change // 5),"Coin")
        change %= 5
    elif change // 2 >= 1:
        print("Change 2 Baht : " + str(change // 2),"Cash")
        change %= 2
    elif change // 1 >= 1:
        print("Change 1 Baht : " + str(change // 1),"Cash")
        change %= 1
        break
    pay = int(input("Payment amount : "))
เป็นโปรแกรมขายตั๋ว โดยมี 2 ราคา แยกจากอายุค่ะ 1-12 = 60 บาท , 13-99 = 120 บาท แล้วก็มีเงินทอนค่ะ

คือว่าพอลองรัน แล้วใส่ค่าอายุเป็น >= 13 มันจะไปคิดตรงเงินทอนของราคาเด็ก 60 บาทอะค่ะ (ควรจะคิดราคา 120 บาท ลองใช้ break ใน while
แล้วใส่คำสั่งเพิ่มเข้าไปก็ไม่ได้ค่ะ)

รบกวนช่วยดูให้หน่อยนะคะ ขอบคุณค่ะ

ข้างบน