NickMrg έγραψε:
δεν είναι το ίδιο με αυτό που μου πρότεινε ο Qdata;
Τσου

Ας αναλύσουμε βήμα βήμα τον κώδικα σου:
- Κώδικας: Επιλογή όλων
fname = input('Please write your first name:')
lname = input('Please write your last name:')
number = input('Please write your phone number:')
Έθεσες κάποιες τιμές για το fname, lname, number. Πολύ ωραία!
- Κώδικας: Επιλογή όλων
if fname or lname or number == '':
print('Do not leave any fields empty')
Με τον τρόπο που το έγραψες κάνεις τρεις έλεγχους, ένα για το fname (ελέγχεις αν υπάρχει
οποιαδήποτε τιμή για το variable fname) και ένα για το lname (ελέγχεις αν υπάρχει οποιαδήποτε τιμή για το variable lname). Τέλος, ελέγχεις αν το number δεν έχει τιμή (είναι κενό με άλλα λόγια ή "")
- Κώδικας: Επιλογή όλων
if fname:
print('Do not leave any fields empty')
Όταν δεν ορίσεις ποια τιμή να ελέγξει, ελέγχει πολλά πράγματα. Καλύτερα να σου το δείξω με παραδείγματα.
A. Αριθμοί: Οποιαδήποτε τιμή (1,2,-2,-100) εκτελεί το if. Το μηδέν (0) εκτελεί το else.
έγραψε:
>>> a = 1
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed if command
>>> a = -20
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed if command
>>> a = 0
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed else command
Β. True εκτελεί το if, False εκτελεί το else.
έγραψε:>>> a = True
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed if command
>>> a = False
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed else command
Γ. Οποιοδήποτε text string ("String") εκτελεί το if. Αν είναι κενό (""), εκτελεί το else.
έγραψε:>>> a = "Linux rules!"
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed if command
>>> a = ""
>>> if a: print("Executed if command")
... else: print("Executed else command")
...
Executed else command
Ξαναπάμε στον κώδικα σου:
- Κώδικας: Επιλογή όλων
if fname or lname or number == '':
print('Do not leave any fields empty')
έγραψε:Σημαίνει:
Αν το fname έχει οποιαδήποτε τιμή (δεν είναι κενό "")
ή
Αν το lname έχει οποιαδήποτε τιμή (δεν είναι κενό "")
ή
Αν το number είναι κενό ("")
Εκτέλεσε: print('Do not leave any fields empty')
Συμπέρασμα: Ζητάς κάτι διαφορετικό από αυτό που προγραμμάτισες να κοιτάξει ο υπολογιστής.
Αφού κατανοήσεις τα παραπάνω, ο σωστός τρόπος είναι:
- Κώδικας: Επιλογή όλων
if not fname or not lname or not number:
print('Do not leave any fields empty')
Με το
not ζητάμε το κενό text string ("").
Ακόμα πιο σωστός τρόπος θα ήταν:
- Κώδικας: Επιλογή όλων
if fname == "" or lname == "" or number == "":
print('Do not leave any fields empty')