Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

2.5 Λήψη Εισόδου από τον Χρήστη στην Python

Η ενσωματωμένη συνάρτηση input() στην Python χρησιμοποιείται για να ζητήσετε και να λάβετε είσοδο από τον χρήστη. Όταν καλείται η input(), το πρόγραμμα παύει την εκτέλεση και περιμένει τον χρήστη να πληκτρολογήσει κάτι. Μόλις ο χρήστης πατήσει Enter, το κείμενο που εισήχθη επιστρέφεται από την συνάρτηση και μπορεί να αποθηκευτεί σε μια μεταβλητή.

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
user_input = input("Please enter something: ")
print("You just typed:", user_input)
user_input = input("Please enter something: ") print("You just typed:", user_input)
user_input = input("Please enter something: ")
print("You just typed:", user_input)

Σε αυτό το παράδειγμα, το πρόγραμμα θα εμφανίσει το μήνυμα “Please enter something: ” και θα περιμένει για την είσοδο του χρήστη. Μετά την είσοδο και το πάτημα του Enter, η είσοδος θα αποθηκευτεί στη μεταβλητή user_input και θα εκτυπωθεί.

Η Συνάρτηση input Πάντα Επιστρέφει ένα String

Όταν χρησιμοποιείτε την συνάρτηση input() στην Python, τα δεδομένα που εισάγει ο χρήστης επιστρέφονται πάντα ως string, ανεξάρτητα από το αν ο χρήστης εισάγει αριθμούς ή κείμενο. Αυτό μπορεί να οδηγήσει σε παρανοήσεις, ειδικά όταν οι τιμές που εισάγονται προορίζονται για αριθμητικές πράξεις.

Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
value1 = input('Enter first number: ')
# Ο χρήστης εισάγει: 7
value2 = input('Enter second number: ')
# Ο χρήστης εισάγει: 3
value1 + value2
value1 = input('Enter first number: ') # Ο χρήστης εισάγει: 7 value2 = input('Enter second number: ') # Ο χρήστης εισάγει: 3 value1 + value2
value1 = input('Enter first number: ')
# Ο χρήστης εισάγει: 7

value2 = input('Enter second number: ')
# Ο χρήστης εισάγει: 3

value1 + value2

Σε αυτή την περίπτωση, η Python δεν προσθέτει τους αριθμούς 7 και 3 για να παράγει 10, αλλά αντ’ αυτού συνενώνει τις συμβολοσειρές ‘7’ και ‘3’, παράγοντας τη συμβολοσειρά ’73’. Αυτό είναι γνωστό ως συνένωση συμβολοσειρών (string concatenation) και δημιουργεί μια νέα συμβολοσειρά που περιέχει την τιμή του αριστερού τελεστέου ακολουθούμενη από την τιμή του δεξιού τελεστέου.

Λήψη Ακέραιας Τιμής από τον Χρήστη στην Python

Όταν χρειάζεστε να λάβετε μια ακέραια τιμή από τον χρήστη, μπορείτε να μετατρέψετε τη συμβολοσειρά που λαμβάνετε από την συνάρτηση input() σε ακέραιο αριθμό χρησιμοποιώντας την ενσωματωμένη συνάρτηση int().

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
value = input('Enter an integer: ')
# Ο χρήστης εισάγει: 7
value = int(value)
value = input('Enter an integer: ') # Ο χρήστης εισάγει: 7 value = int(value)
value = input('Enter an integer: ')
# Ο χρήστης εισάγει: 7

value = int(value)

Μετά από αυτό, η μεταβλητή value περιέχει τον ακέραιο αριθμό 7. Επίσης, μπορείτε να συνδυάσετε τις δύο παραπάνω γραμμές κώδικα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
another_value = int(input('Enter another integer: '))
# Ο χρήστης εισάγει: 13
another_value = int(input('Enter another integer: ')) # Ο χρήστης εισάγει: 13
another_value = int(input('Enter another integer: '))
# Ο χρήστης εισάγει: 13

Τώρα, η μεταβλητή another_value περιέχει τον ακέραιο αριθμό 13. Οι μεταβλητές value και another_value περιέχουν ακέραιες τιμές και η πρόσθεσή τους θα παράγει ένα ακέραιο αποτέλεσμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
result = value + another_value
result = value + another_value
result = value + another_value

Το αποτέλεσμα της παραπάνω εντολής θα είναι 20, δεδομένου ότι η value είναι 7 και η another_value είναι 13.

Σημειώστε ότι αν η συμβολοσειρά που περνάτε στη συνάρτηση int() δεν μπορεί να μετατραπεί σε ακέραιο, θα προκληθεί ένα σφάλμα ValueError. Για παράδειγμα, αν ο χρήστης εισάγει έναν αριθμό που δεν είναι ακέραιος ή ένα μη αριθμητικό κείμενο, η μετατροπή θα αποτύχει και θα εμφανιστεί ένα σφάλμα.

10 Ιανουαρίου, 2024
top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων