A brief note on Talking to the User: input() and changing text into numbers, positions and slices, string methods, f-strings, and running Python on your own computer. With a worked example, practice, a program and questions to check yourself.
A brief note on Talking to the User. Read it before a test. Try each question at the end before you open its answer.
input("question") shows the question, waits for an answer and gives the answer back. Keep it in a variable: city = input("Your city? ").input() reads the next line.EOFError.input() always gives text, even when the user types digits.int() makes a whole number, float() a decimal number and str() text. int(input("How many? ")) asks and converts in one line.len() counts them, spaces too.-1 is the last character.IndexError.word[start:stop] takes the characters from start up to, but not including, stop.With word = "GARDEN":
| Code | Gives |
|---|---|
word[0] | "G" |
word[-1] | "N" |
word[1:4] | "ARD" |
word[:2] | "GA" |
word[::-1] | "NEDRAG" |
upper(), lower(), title(), strip() and replace() give a new string. Store it if you want to keep it: name = name.strip().in, startswith(), endswith(), isdigit() and isalpha() answer True or False.count() counts a piece. find() gives the position of the first match, or -1 when it is not there.+ joins strings with no space between them. * repeats a string: "=" * 10.f"{name} scored {marks}". No str() is needed.{x:.2f} shows two decimal places. {x:>8} puts the value on the right of 8 spaces, and {x:<8} on the left.py --version, or python3 --version on a Mac..py. Run it in IDLE with F5, or in a terminal with python3 hello.py from the folder of the file.cd goes into a folder. python -m pip install name adds a package.Dev wants a program that asks for the price of one notebook and the number of notebooks, then shows the bill.
input() gives text, so the price goes through float() and the number through int().bill = price * count.:.2f gives the bill in rupees with two decimal places.Without float() and int(), the third line would try to multiply two pieces of text. Python stops with TypeError: can't multiply sequence by non-int of type 'str'.
The program makes a username from the first four letters of a first name and the last two digits of a year.
input("Age? ") + 1 stops with TypeError: can only concatenate str (not "int") to str. Convert first: int(input("Age? ")).int() on a decimal. int("12.5") stops with ValueError: invalid literal for int() with base 10: '12.5'. Use float() for a decimal number.word[0]. In a word of 3 letters, word[3] stops with IndexError: string index out of range.name.upper() on its own line changes nothing. Write name = name.upper().word[0] = "B" stops with TypeError: 'str' object does not support item assignment. Build a new string: "B" + word[1:].hello.py.txt, or random.py. The first does not run as a program. The second hides the module of the same name.age = input("Age? ") and then print(age + 5). What happens, and how should she change the first line?word = "LANTERN", what are word[2], word[-2] and word[2:5]?fee = 37.5. Which f-string shows it as Fee: 37.50 rupees?"banana".find("n") and "banana".count("a") give?marks.py sits in a folder called python_work, but the terminal is open in another folder. Which two steps run the program?Log in to join the discussion.
Loading…