#data types and type checking
#type conversion
num_char=len(input("What is your name?"))
print("Your name has "+num_char+" charaters")
TypeError: can only concatenate str (not "int") to str
num_char=len(input("What is your name?"))
new_num_char=str(num_char)
print("Your name has "+new_num_char+" charaters")
#type checking
a=str(123)
print(type(a))
print(70+float("100.5"))
print(str(70)+str(100))
#2.1
two_digit_number=input("Type a two digit number:")
print(type(two_digit_number))
first_number=int(two_digit_number[0])
second_number=int(two_digit_number[1])
result=first_number+second_number
print(result)
#mathematical operations
#PEMDAS(from left to right)
#() bracket or parentheses
# ** exponents
# * / multiplication division
# + - addition subtraction
#2.2 BMI calculator
height=float(input("enter your height in m:"))
weight=int(input("enter your weight in kg:"))
print(type(height))
bmi=weight/height**2
bmi_as_int=int(bmi)
print(bmi_as_int)
#round numbers
#print(round(8/3)) 3
#print(round(8/3,2)) 2.67
#print(8//3) 2
#print(type(8/3)) float
#print(type(8//3)) int
#result=4/2
#result/=2 #+= *= -=
#print(result)
#f-String
#所有不同的数据类型通过使用f组合成一个字符串,使用花括号来放置变量
score=0
height=1.8
isWinning=True
print(f"your score is {score},your height is {height},you are winning is {isWinning}")
#2.3 Your Life in Weeks
age = input ("What is your current age?")
age_as_int=int(age)
years_remaining=90-age_as_int
days_remaining=years_remaining*365
weeks_remaining=years_remaining*52
months_remaining=years_remaining*12
message=f"You have {days_remaining} days,{weeks_remaining} weeks,and {months_remaining} months left."
print(message)
#Project_Tip Calculator
print("Welcome to the tip calculator.")
bill=float(input("What was the total bill?$"))
tip=int(input("What percentage tip would you like to give?10,12or15?"))
person=int(input("How many people to spilt the bill?"))
total_bill=bill*(1+tip/100)
bill_per_person=total_bill/person
#final_amount=round(bill_per_person,2)
final_amount="{:.2f}".format(bill_per_person)
#格式化:把一个浮点数变成了一个字符串,并且该字符串遵循这种特殊格式,即两位小数
print(f"Each person should par:${final_amount}")
Day2_Udemy Python 100days
于 2023-08-08 13:09:43 首次发布