Dapatkah Anda memiliki 2 syarat dalam for loop python?

Pernyataan if Python sederhana hanya menguji satu syarat. Kondisi itu kemudian menentukan apakah kode kita berjalan (

Current temperature (30.2) is between high and low extremes.
4) atau tidak (
Current temperature (30.2) is between high and low extremes.
5). Jika kita ingin mengevaluasi skenario yang lebih kompleks, kode kita harus menguji beberapa kondisi secara bersamaan. Mari kita lihat bagaimana kita mengkodekannya dengan Python

DALAM ARTIKEL INI

Uji beberapa kondisi dengan satu pernyataan if Python

Untuk menguji beberapa kondisi dalam

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7 atau
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
8 klausa kami menggunakan apa yang disebut operator logis. Operator ini menggabungkan beberapa nilai benar/salah menjadi hasil akhir
Current temperature (30.2) is between high and low extremes.
4 atau
Current temperature (30.2) is between high and low extremes.
5 (Sweigart, 2015). Hasil itu mengatakan bagaimana kondisi kita digabungkan, dan itu menentukan apakah pernyataan if kita berjalan atau tidak

Kami mengevaluasi beberapa kondisi dengan dua operator logis (Lutz, 2013; Python Docs, n. d. )

  • Operator
    Current temperature (30.2) is between high and low extremes.
    
    2 mengembalikan
    Current temperature (30.2) is between high and low extremes.
    
    4 ketika kondisi kiri dan kanannya juga
    Current temperature (30.2) is between high and low extremes.
    
    4. Ketika salah satu atau kedua kondisi
    Current temperature (30.2) is between high and low extremes.
    
    _5, hasil yang
    Current temperature (30.2) is between high and low extremes.
    
    2 adalah
    Current temperature (30.2) is between high and low extremes.
    
    5 juga
  • Operator
    Current temperature (30.2) is between high and low extremes.
    
    3 mengembalikan
    Current temperature (30.2) is between high and low extremes.
    
    4 ketika kondisi kiri, kanan, atau keduanya adalah
    Current temperature (30.2) is between high and low extremes.
    
    4. Satu-satunya saat
    Current temperature (30.2) is between high and low extremes.
    
    3 mengembalikan
    Current temperature (30.2) is between high and low extremes.
    
    5 adalah ketika kedua kondisi
    Current temperature (30.2) is between high and low extremes.
    
    5 juga

Jangan khawatir jika ini terdengar abstrak atau tidak jelas; . Omong-omong, mari kita lihat contoh-contoh itu

Beberapa Current temperature (30.2) is between high and low extremes. 4 kondisi dalam pernyataan if. operator Current temperature (30.2) is between high and low extremes. _2

Ketika pernyataan if membutuhkan beberapa

Current temperature (30.2) is between high and low extremes.
_4 kondisi pada saat yang sama, kami menggabungkan kondisi yang berbeda tersebut bersama dengan operator
Current temperature (30.2) is between high and low extremes.
2. Kondisi gabungan seperti itu menjadi
Current temperature (30.2) is between high and low extremes.
_5 segera setelah satu kondisi diuji
Current temperature (30.2) is between high and low extremes.
5. Mari kita lihat beberapa contoh

Jika pernyataan yang membutuhkan dua Current temperature (30.2) is between high and low extremes. 4 kondisi

Jadi saat kita menggabungkan kondisi dengan

Current temperature (30.2) is between high and low extremes.
2, keduanya harus
Current temperature (30.2) is between high and low extremes.
4 pada waktu yang sama. Inilah contoh pernyataan if itu

# Current temperature
currentTemp = 30.2

# Extremes in temperature (in Celsius)
tempHigh = 40.7
tempLow = -18.9

# Compare current temperature against extremes
if currentTemp > tempLow and currentTemp < tempHigh:
    print('Current temperature (' + str(currentTemp) +
          ') is between high and low extremes.')

Pertama kita buat variabel

Temperature (40.7) is above record low or below record high.
_2 dengan suhu saat ini. Kemudian kita membuat dua variabel lainnya,
Temperature (40.7) is above record low or below record high.
3 dan
Temperature (40.7) is above record low or below record high.
4. Itu mewakili rekor sepanjang masa untuk stasiun cuaca tertentu

Sekarang kami ingin tahu apakah suhu saat ini berada di antara ekstrem tersebut. Jadi kami memiliki pernyataan if yang menguji dua kondisi. Yang pertama melihat apakah suhu di atas rekor rendah (

Temperature (40.7) is above record low or below record high.
5). Yang lain terlihat jika suhunya di bawah rekor tinggi (
Temperature (40.7) is above record low or below record high.
6)

Kami menggabungkan ketentuan tersebut dengan operator

Current temperature (30.2) is between high and low extremes.
2. Itu membuat pernyataan if kami hanya berjalan ketika keduanya
Current temperature (30.2) is between high and low extremes.
4. Sejak itu, kode itu dijalankan dan memiliki
Temperature (40.7) is above record low or below record high.
9 menampilkan yang berikut ini

Current temperature (30.2) is between high and low extremes.

Jika pernyataan yang membutuhkan beberapa Current temperature (30.2) is between high and low extremes. 4 kondisi

Operator

Current temperature (30.2) is between high and low extremes.
2 dapat menggabungkan kondisi sebanyak yang diperlukan. Karena setiap kondisi yang kita tambahkan dengan
Current temperature (30.2) is between high and low extremes.
_2 mencari hal tertentu, pernyataan if kita dapat berjalan dalam situasi yang sangat spesifik

Katakanlah sebuah restoran cepat saji menawarkan 4 tambahan opsional kepada pelanggan untuk setiap pesanan. Jika kode kami akan terlihat jika seseorang memesan keempat ekstra, kami melakukannya

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")

Pertama kita membuat empat variabel benar/salah (

# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
3,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
4,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
5, dan
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
6). Itu mewakili tambahan apa yang diinginkan pelanggan

Kemudian kami membuat kode pernyataan if/else. Untuk menjalankan kode

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7, empat kondisi harus
Current temperature (30.2) is between high and low extremes.
4 pada waktu yang sama. Itu karena kita menggabungkan keempat variabel benar/salah dengan operator
Current temperature (30.2) is between high and low extremes.
2. Sayangnya, salah satunya adalah
Current temperature (30.2) is between high and low extremes.
5.
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
5 karena pelanggan tidak menginginkan milkshake

Itu membuat seluruh kondisi yang diuji

Current temperature (30.2) is between high and low extremes.
5 juga. Jadi kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7 tidak berjalan, tetapi kode
Optional extras for order:
No salt:		 True
Diet coke:		 False
French fries:	 False
Milkshake:		 False
4 berjalan. Di sana fungsi
Temperature (40.7) is above record low or below record high.
_9 mengatakan bahwa pelanggan tidak menginginkan keempat tambahan tersebut

The customer doesn't want diet coke, extra fries, a milkshake, *and* an extra burger.

Satu kondisi Current temperature (30.2) is between high and low extremes. _4 dalam pernyataan if. operator Current temperature (30.2) is between high and low extremes. _3

Pilihan lainnya adalah operator

Current temperature (30.2) is between high and low extremes.
_3. Saat kami menggabungkan kondisi dengan operator tersebut, hanya satu yang harus
Current temperature (30.2) is between high and low extremes.
4 untuk membuat seluruh kombinasi
Current temperature (30.2) is between high and low extremes.
4. Hanya ketika setiap kondisi
Current temperature (30.2) is between high and low extremes.
_5 apakah pernyataan if kita menguji
Current temperature (30.2) is between high and low extremes.
5 juga. Mari kita lihat beberapa contohnya

Jika pernyataan yang membutuhkan hanya satu dari dua kondisi

Jadi saat kita menggabungkan kondisi dengan

Current temperature (30.2) is between high and low extremes.
3, hanya satu yang harus
Current temperature (30.2) is between high and low extremes.
4. Inilah cara kita dapat menggunakan perilaku itu dengan pernyataan if

# Current temperature
currentTemp = 40.7

# Extremes in temperature (in Celsius)
tempHigh = 40.7
tempLow = -18.9

# Compare current temperature against extremes
if currentTemp > tempLow or currentTemp < tempHigh:
    print('Temperature (' + str(currentTemp) +
          ') is above record low or ' +
          'below record high.')
else:
    print("There's a new record-breaking temperature!")

Kami pertama kali membuat tiga variabel.

Temperature (40.7) is above record low or below record high.
2 memiliki pembacaan suhu saat ini;

Pernyataan if/else kemudian membandingkan suhu saat ini dengan suhu ekstrim tersebut. Bagian

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7 memeriksa dua kondisi. Pertama kita lihat apakah suhu saat ini di atas titik terendah sepanjang masa (
Temperature (40.7) is above record low or below record high.
5). Kemudian kami memeriksa apakah suhunya di bawah pembacaan tertinggi (
Temperature (40.7) is above record low or below record high.
6)

Karena kami menggabungkan kedua kondisi tersebut dengan operator

Current temperature (30.2) is between high and low extremes.
3, hanya satu yang harus menguji
Current temperature (30.2) is between high and low extremes.
4 sebelum Python menjalankan kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7. Karena suhu saat ini di atas minimum (tetapi tidak di bawah maksimum), seluruh kondisi kami menguji
Current temperature (30.2) is between high and low extremes.
4 berkat
Current temperature (30.2) is between high and low extremes.
3

Jadi kode

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7 dijalankan. Di sana fungsi
Temperature (40.7) is above record low or below record high.
_9 mengatakan suhu saat ini di atas rekor terdingin atau terpanas

Temperature (40.7) is above record low or below record high.

Jika pernyataan yang membutuhkan satu Current temperature (30.2) is between high and low extremes. 4 kondisi di antara beberapa

Dengan operator

Current temperature (30.2) is between high and low extremes.
_3 kita dapat menggabungkan kondisi sebanyak yang dibutuhkan. Ketika kita melakukannya, kita masih membutuhkan hanya satu
Current temperature (30.2) is between high and low extremes.
4 kondisi untuk membuat seluruh kombinasi
Current temperature (30.2) is between high and low extremes.
4 juga. Ini biasanya berarti bahwa semakin banyak kondisi yang kita gabungkan dengan
Current temperature (30.2) is between high and low extremes.
3, semakin besar kemungkinan seluruh kondisi tersebut adalah
Current temperature (30.2) is between high and low extremes.
4

Berikut adalah contoh program yang menguji beberapa kondisi

Current temperature (30.2) is between high and low extremes.
3

# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")

Program ini menangani pesanan pelanggan di restoran cepat saji. Kami pertama-tama membuat empat variabel (

Current temperature (30.2) is between high and low extremes.
05,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
3,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
4, dan
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
5). Masing-masing mendapat
Current temperature (30.2) is between high and low extremes.
4 atau
Current temperature (30.2) is between high and low extremes.
5 berdasarkan apa yang dipesan pelanggan

Kemudian kami memproses pesanan itu dengan pernyataan if/else. Bagian

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7 menggabungkan empat variabel dengan operator
Current temperature (30.2) is between high and low extremes.
3 menjadi satu kondisi. Jadi hanya satu variabel
Current temperature (30.2) is between high and low extremes.
4 sudah cukup untuk menjalankan kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7. Dan benar saja, satu variabel (
Current temperature (30.2) is between high and low extremes.
_05) memang
Current temperature (30.2) is between high and low extremes.
4

Meskipun semua variabel lainnya adalah

Current temperature (30.2) is between high and low extremes.
_5, satu variabel
Current temperature (30.2) is between high and low extremes.
4 itu cukup untuk menjalankan kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7. Di sana
Temperature (40.7) is above record low or below record high.
_9 menampilkan apa yang dipesan pelanggan dengan mengeluarkan nilai dari setiap variabel

Optional extras for order:
No salt:		 True
Diet coke:		 False
French fries:	 False
Milkshake:		 False

Kondisi kompleks dalam pernyataan if Python. Current temperature (30.2) is between high and low extremes. 2 + Current temperature (30.2) is between high and low extremes. 3

Untuk menangani skenario yang kompleks, pernyataan if kami dapat menggabungkan operator

Current temperature (30.2) is between high and low extremes.
2 dan
Current temperature (30.2) is between high and low extremes.
3 bersama-sama. Dengan cara itu kami mengubah beberapa kondisi menjadi kode, yang beberapa di antaranya harus terjadi secara bersamaan (
Current temperature (30.2) is between high and low extremes.
2) sementara yang lain hanya perlu satu untuk menjadi
Current temperature (30.2) is between high and low extremes.
4 (
Current temperature (30.2) is between high and low extremes.
3)

Saat kita membuat kode kondisi kompleks, sebaiknya gunakan tanda kurung (

Current temperature (30.2) is between high and low extremes.
28 dan
Current temperature (30.2) is between high and low extremes.
29). Terkadang mereka diminta untuk mengubah urutan operasi Python. Dan di lain waktu mereka hanya membuat kode lebih mudah dipahami

Mari kita lihat bagaimana menggabungkan kondisi dengan

Current temperature (30.2) is between high and low extremes.
2 dan
Current temperature (30.2) is between high and low extremes.
3 terlihat. Ini contoh singkatnya

condition = (A and B) or C

Kondisi gabungan ini menguji

Current temperature (30.2) is between high and low extremes.
_4 dalam salah satu dari dua skenario

  • Ketika kombinasi dari
    Current temperature (30.2) is between high and low extremes.
    
    33 dan
    Current temperature (30.2) is between high and low extremes.
    
    34 adalah
    Current temperature (30.2) is between high and low extremes.
    
    4
  • Atau ketika
    Current temperature (30.2) is between high and low extremes.
    
    _36 adalah
    Current temperature (30.2) is between high and low extremes.
    
    4

Ketika kondisi pertama dan kedua adalah

Current temperature (30.2) is between high and low extremes.
5, maka kombinasi ini juga
Current temperature (30.2) is between high and low extremes.
5

Ini contoh lainnya

condition = (A or B) and C

Kombinasi ini adalah

Current temperature (30.2) is between high and low extremes.
4 ketika dua hal terjadi pada waktu yang sama

  • Baik
    Current temperature (30.2) is between high and low extremes.
    
    _33 atau
    Current temperature (30.2) is between high and low extremes.
    
    34 adalah
    Current temperature (30.2) is between high and low extremes.
    
    4
  • Dan
    Current temperature (30.2) is between high and low extremes.
    
    _36 tes
    Current temperature (30.2) is between high and low extremes.
    
    4

Ketika

Current temperature (30.2) is between high and low extremes.
33 dan
Current temperature (30.2) is between high and low extremes.
34 digabungkan menjadi
Current temperature (30.2) is between high and low extremes.
5, dan
Current temperature (30.2) is between high and low extremes.
36 adalah
Current temperature (30.2) is between high and low extremes.
5, maka kondisi gabungannya adalah
Current temperature (30.2) is between high and low extremes.
5 juga. Sekarang mari pertimbangkan beberapa contoh program Python untuk mempelajari lebih lanjut

Contoh. jika pernyataan dengan Current temperature (30.2) is between high and low extremes. 2 + Current temperature (30.2) is between high and low extremes. 3 kondisi

Katakanlah program kita menangani pesanan di restoran cepat saji. Untuk menugaskan anggota staf yang tepat untuk memesan, kita harus tahu apakah pelanggan menginginkan minuman atau makanan tambahan. Kami mengevaluasinya dengan pernyataan if/else

Current temperature (30.2) is between high and low extremes.
0

Kami pertama kali membuat empat variabel.

# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
3,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
5,
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
4, dan
Current temperature (30.2) is between high and low extremes.
57. Masing-masing menunjukkan jika pelanggan menginginkan tambahan tertentu (
Current temperature (30.2) is between high and low extremes.
4) atau tidak (
Current temperature (30.2) is between high and low extremes.
5)

Kemudian kami memproses pesanan dengan pernyataan if/else. Di sana kami mengevaluasi dua kelompok kondisi, digabungkan dengan

Current temperature (30.2) is between high and low extremes.
2. Itu berarti kedua grup harus
Current temperature (30.2) is between high and low extremes.
_4 sebelum kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
7 berjalan

Kelompok pertama melihat apakah pelanggan memesan diet coke atau milkshake (

Current temperature (30.2) is between high and low extremes.
63). Karena kita menggabungkan ekspresi tersebut dengan
Current temperature (30.2) is between high and low extremes.
_3, hanya satu yang harus
Current temperature (30.2) is between high and low extremes.
4 untuk membuat grup ini
Current temperature (30.2) is between high and low extremes.
4. (Karena
# Check which extras the customer ordered
noSalt = True
dietCoke = False
fries = False
shake = False

# Handle the customer's order
if noSalt or dietCoke or fries or shake:
    print("Optional extras for order:")
    print("No salt:\t\t", noSalt)
    print("Diet coke:\t\t", dietCoke)
    print("French fries:\t", fries)
    print("Milkshake:\t\t", shake)
else:
    print("No extras needed for this order. Please proceed.")
5 adalah
Current temperature (30.2) is between high and low extremes.
4, hasilnya memang
Current temperature (30.2) is between high and low extremes.
4. )

Sekarang untuk kelompok kedua. Di sini kita melihat apakah pelanggan memesan kentang goreng ekstra atau burger (

Current temperature (30.2) is between high and low extremes.
70). Sekali lagi kami menggunakan operator
Current temperature (30.2) is between high and low extremes.
3 sehingga satu nilai
Current temperature (30.2) is between high and low extremes.
4 sudah cukup untuk membuat grup ini
Current temperature (30.2) is between high and low extremes.
4. (Karena keduanya
Current temperature (30.2) is between high and low extremes.
_4, hasilnya adalah
Current temperature (30.2) is between high and low extremes.
4 juga. )

Karena grup kiri dan kanan keduanya

Current temperature (30.2) is between high and low extremes.
_4, bergabung dengan mereka dengan
Current temperature (30.2) is between high and low extremes.
2 memberikan nilai
Current temperature (30.2) is between high and low extremes.
4 juga. Dan kode
# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7 berjalan. Di sana fungsi
Temperature (40.7) is above record low or below record high.
_9 mengatakan tambahan mana yang diinginkan pelanggan

Current temperature (30.2) is between high and low extremes.
_1

Perhatikan bahwa kami tidak terlalu tepat tentang apa yang diinginkan pelanggan. Karena beberapa situasi dapat memicu kode

# Check which extras the customer ordered
dietCoke = True
fries = True
shake = False
extraBurger = True

if dietCoke and fries and shake and extraBurger:
    print("The customer wants:")
    print("- Diet instead of regular coke")
    print("- Extra french fries")
    print("- A milkshake")
    print("- An extra burger")
else:
    print("The customer doesn't want diet coke, " +
          "extra fries, a milkshake, *and* an extra burger.")
_7, kami tidak dapat mengatakan apa yang membuat kode tersebut berjalan. Ini adalah konsekuensi dari operator ________0______3

Secara umum, semakin banyak kondisi yang Anda gabungkan dengan

Current temperature (30.2) is between high and low extremes.
3, semakin kurang tepat Anda tentang apa yang menyebabkan kode berjalan

Cara lain untuk menangani kondisi pernyataan if

Selain menguji beberapa skenario, ada cara lain untuk membuat kode kondisi if

  • Dalam membandingkan nilai dengan pernyataan if, kami menjelajahi bagaimana kami membuat kode skenario lebih besar dari dan lebih kecil dari
  • Dalam negasi logis dengan pernyataan if kita membahas bagaimana kode dapat memeriksa apakah situasi tertentu tidak terjadi
  • Dan dalam if statement membership test kita memiliki operator
    Current temperature (30.2) is between high and low extremes.
    
    84 menguji apakah beberapa nilai ada dalam nilai lain

Untuk informasi lebih lanjut tentang pernyataan if Python, lihat kategori pernyataan if

Ringkasan

Untuk mengevaluasi skenario yang kompleks, kami menggabungkan beberapa kondisi dalam pernyataan if yang sama. Python memiliki dua operator logis untuk itu

Operator

Current temperature (30.2) is between high and low extremes.
2 mengembalikan
Current temperature (30.2) is between high and low extremes.
4 ketika kondisi di kiri dan kanannya sama-sama
Current temperature (30.2) is between high and low extremes.
4. Jika salah satu atau keduanya adalah
Current temperature (30.2) is between high and low extremes.
_5, maka kombinasinya juga
Current temperature (30.2) is between high and low extremes.
5. Itu memprogram skenario yang ketat. hanya ketika beberapa kondisi
Current temperature (30.2) is between high and low extremes.
_4 pada saat yang sama pernyataan if kita akan berjalan

Operator

Current temperature (30.2) is between high and low extremes.
3 berbeda. Yang ini mengembalikan
Current temperature (30.2) is between high and low extremes.
4 ketika kondisi kiri dan/atau kanannya adalah
Current temperature (30.2) is between high and low extremes.
4. Hanya dengan
Current temperature (30.2) is between high and low extremes.
5 kombinasi
Current temperature (30.2) is between high and low extremes.
3 juga mengembalikan
Current temperature (30.2) is between high and low extremes.
5. Itu membuat pernyataan if kami lebih fleksibel. sekarang satu nilai
Current temperature (30.2) is between high and low extremes.
4 sudah cukup untuk menjalankan kodenya

Untuk skenario kompleks, kami menggabungkan operator

Current temperature (30.2) is between high and low extremes.
_2 dan
Current temperature (30.2) is between high and low extremes.
3. Dengan tanda kurung, kami kemudian mengklarifikasi kode kami dan menentukan bagaimana Python harus memproses kondisi yang berbeda

Referensi

Lutz, M. (2013). Belajar Python (Edisi ke-5). Sebastopol, CA. O'Reilly Media

Piton. org (n. d. ). Ekspresi. Diakses pada 5 Agustus 2019, dari https. //dokumen. python. org/3/referensi/ekspresi. html

Swigart, A. (2015). Otomatiskan Hal-Hal yang Membosankan Dengan Python. Pemrograman Praktis untuk Pemula Total. San Fransisco, CA. Tanpa Pers Pati

Diterbitkan 6 September 2019

Tutorial Python terkait

  • Bandingkan nilai dengan pernyataan if Python. sama, tidak sama, lebih besar dan lebih kecil dari

    Pernyataan if Python dapat membandingkan nilai untuk sama, tidak sama, lebih besar dan lebih kecil dari. Artikel ini menjelaskan kondisi tersebut dengan banyak contoh

  • Pernyataan if bersarang Python.
    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    7 kode di dalam pernyataan if lainnya

    Pernyataan if bersarang adalah klausa

    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    _7 yang ditempatkan di dalam blok kode
    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    7 atau
    Optional extras for order:
    No salt:		 True
    Diet coke:		 False
    French fries:	 False
    Milkshake:		 False
    
    4. Mereka memungkinkan pemeriksaan kondisi dan skenario Python yang rumit

  • Pernyataan if mengalir dari Python. menguji beberapa kondisi setelah satu sama lain

    Pernyataan if mengalir dari Python mengevaluasi beberapa kondisi secara berurutan. Ketika seseorang adalah

    Current temperature (30.2) is between high and low extremes.
    
    _4, kode itu berjalan. Jika semuanya
    Current temperature (30.2) is between high and low extremes.
    
    5 kode
    Optional extras for order:
    No salt:		 True
    Diet coke:		 False
    French fries:	 False
    Milkshake:		 False
    
    4 dijalankan

  • Jika pernyataan yang menguji sebaliknya. Penjelasan
    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    _07 Python

    Sebagian besar pernyataan if Python mencari situasi tertentu. Tapi kita juga bisa mengeksekusi kode ketika kondisi tertentu tidak terjadi. Kami melakukannya dengan

    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    _08

  • Pernyataan if/else Python. memilih antara dua opsi secara terprogram

    Pernyataan if/else membuat Python mengambil keputusan. Saat kondisi menguji

    Current temperature (30.2) is between high and low extremes.
    
    _4, kode yang dimaksudkan di bawah
    # Check which extras the customer ordered
    dietCoke = True
    fries = True
    shake = False
    extraBurger = True
    
    if dietCoke and fries and shake and extraBurger:
        print("The customer wants:")
        print("- Diet instead of regular coke")
        print("- Extra french fries")
        print("- A milkshake")
        print("- An extra burger")
    else:
        print("The customer doesn't want diet coke, " +
              "extra fries, a milkshake, *and* an extra burger.")
    7 berjalan. Jika tidak, kode
    Optional extras for order:
    No salt:		 True
    Diet coke:		 False
    French fries:	 False
    Milkshake:		 False
    
    _4 dijalankan

    Bagaimana Anda memiliki dua kondisi dalam for loop dengan Python?

    Python While Loop dengan Berbagai Kondisi Menggunakan AND . Mari kita lihat seperti apa ini. a = 0 b = 10 sedangkan a < 4 dan b > 3. print(halo. Nilai a adalah {a} dan nilai b adalah {b}. place the and keyword between each of the conditions. Let's take a look at what this looks like: a = 0 b = 10 while a < 4 and b > 3: print(f'Hello! The value of a is {a} and the value of b is {b}.

    Bisakah kita menggunakan 2 Kondisi untuk loop?

    Itu mengatakan bahwa hanya satu kondisi yang diperbolehkan dalam loop for, namun Anda dapat menambahkan beberapa kondisi dalam loop for dengan menggunakan operator logika untuk menghubungkannya.

    Bisakah Anda memiliki dua kondisi dalam pernyataan if Python?

    Dengan Python, kita dapat menggunakan operator logika (mis. e. , dan, atau) untuk menggunakan beberapa ketentuan dalam pernyataan if yang sama . Lihatlah kode di bawah ini.