if語法練習
1、編寫一個輸入分數,輸出分數等級的程式,具體為:
Score Grade
90~100 A
70~89 B
60~69 C
0~59 D
others Invalid score
請添加必要的輸入輸出語句,儘量讓程式友好。
if __name__ == '__main__':
score = int(input('please input your score:'))
if score >= 90:
print('Your grade is A!')
elif score >= 70:
print('Your grade is B!')
elif score >= 60:
print('Your grade is C!')
elif score >=0:
print('Your grade is D!')
else:
print('Invalid score!')
```
2、編寫程式,從鍵盤輸入一個二元一次方程ax^2+bx+c=0的三個參數a、b、c(均為整數),求此方程的實根。如果方程有實根,則輸出實根(保留一位小數),如果沒有實根則輸出沒有實根的資訊。
if __name__ == '__main__':
a, b, c = eval(input("please input a,b,c values of a*x^2+b*x+c:"))
t = b ** 2 - 4 * a * c
if t > 0:
x1 = (-b + sqrt(t)) / (2 * a)
x2 = (-b - sqrt(t)) / (2 * a)
print('x1 = {}, x2 = {}'.format(x1, x2))
elif t == 0:
x = -b / (2 * a)
print('x = {}'.format(x))
else:
print('no real solution')