Mastery06-Basic user input

In python 3 we have the ability to perform user input by using the input() function.

If you want the user to enter a value or a string and store it to a variable you can do it in this way:

As python variables are dynamic and can change their type the entered in value will be stored into the variable regardless of what type you might expect.
If you want the values to be a specific type, for example because you want to use them in your program as floats you can use convert functions:

In this case every entered number will be converted to a float number. Those convert functions exist for every data type in python (see mastery04), for example int() etc.

Errors can occur, if the user types in a string instead of a float/integer number. In this case that would be a ValueError.
To avoid this you can use exceptions and force the user to repeat the input until the value is valid. A example of this could be:

If you want a video tutorial for basic user input in python I can recommend the following video:

If you are searching for more advanced stuff like multiple inputs in one line using the split() method this website should be a good choice:

https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/

In my opinion websites like

  • Stackoverflow
  • Stackabuse
  • GeeksForGeeks

are good sources for programming help in general.

Werbeanzeigen

Kommentar hinterlassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden /  Ändern )

Google Foto

Du kommentierst mit Deinem Google-Konto. Abmelden /  Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s

Erstelle deine Website auf WordPress.com
Jetzt starten
%d Bloggern gefällt das: