การใช้ 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 ค่ะ

โดย dos » 05/11/2019 7:55 am

จากที่ดู Code แล้ว ยังไม่มีการเขียนเงื่อนไขของตั๋วเด็กเลยนะครับ
แต่กำหนดเงื่อนไขของราคาผู้ใหญ่ให้คิดแค่ 60 บาทอีกต่างหาก
ดังนั้นจึงดูเหมือนว่าโปรแกรมคิดราค่เด็กตลอด
แต่ความจริงแล้ว โปรแกรมคิดราคาผู้ใหญ่ที่ 60 บาทครับ

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

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

โดย Mind Mild » 03/11/2019 8:26 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 ราคา ตามอายุค่ะ เด็ก 60 ผู้ใหญ๋ 120
ลองรันแล้วตัวราคา 120 ไปคิดเงินทอนกับราคา 60 บาทตลอดเลยค่ะ
ไม่ทราบว่ามีวิธียังไงให้ตัวราคา 120 ไปคิดเงินทอนกับตัวราคาผู้ใหญ่บ้างคะ

ข้างบน