Python: Challenges and Decisions (Chapter 2. Types, Variables, and Basic I/O. Useless Facts Program).

We continue to learn programming. After the second chapter in the book: Michael Dawson “We Program in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I studied the features of working with text in the Python programming language, tasks were offered. Let’s do them together. I will give my solution, and you write your options in the comments.

1) Come up with two lists: valid and invalid variable names. Explain why each of the names is valid or not valid, respectively. Then come up with two more lists—of “good” and “bad” valid names—and explain your choice.

Valid variable names: name, price, password, age, size (these can be any words that are not reserved in the programming language for functions / operators)

Invalid variable names: print, input, int, float, str (you cannot use words that denote commands in a programming language to set variables, otherwise the computer will think that this is a function, not a variable)

Valid “good” variable names: size_square, name_user1, price_food (in Python, it is customary to name variables with a small letter and use an underscore to separate words by meaning, “good” variable names are meaningful, they immediately understand what the speech is about and the names easy to read).

Valid “bad” variable names: sizeb1345iz, nameuserspassword, KotName5 (don’t use different case characters in variable names)

2) Write a program in which the user can enter the names of his two favorite dishes. The program should concatenate these two lines and output the resulting line as the name of a new unseen dish.

print('Программа "Невиданное блюдо"')
bludo_1=input('Напиши свое любимое блюдо.\n')
bludo_2=input('Напиши еще одно свое любимое блюдо.\n')
print('Невиданное блюдо называется:',bludo_1+bludo_2)

3) Write a “Generous Visitor” program that allows the user to enter the amount of the bill for a meal at a restaurant. The program should display two values: tips at the rate of 15% and 20% of the specified amount.

print('Программа "Щедрый посетитель"')
summa_scheta=float(input('Введите сумму счета за обед, руб.:\n'))
print('Чаевые 15%:', int(summa_scheta*0.15),'руб.; чаевые 20%:', int(summa_scheta*0.20),'руб.')

4) Write a program “Car Dealer”, in the window of which the user will be able to enter the cost of the car without extra charges. The program must add to it several additional amounts: tax, registration fee, agency fee, the cost of delivering the car to its destination. Let the tax and registration fee be calculated as a share of the initial cost, and the rest of the margins will be considered as fixed values. The final price of the car should be displayed.

print('Программа "Автодилер"')
price_net=float(input('Стоимость машины без наценок,тыс.руб.:\n'))
print('Окончательная цена=',price_net+nalog+registraciya+agent_sbor+dostavka,'тыс.руб.')

This entry was posted in Python (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs are not allowed.
🇷🇺 Комментарии со ссылками удаляются автоматически.