python練習問題練習(一)
3113 ワード
参考書:pythonプログラミング
chapter 1
5.
chapter 1
5.
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1:"))
n = eval(input("How many times shoule I print?"))
for i in range(n):
x = 3.9 * x * (1 - x)
print(x)
chapter2
- - - - / -
4.
def convert():
celsius = eval(input("What's the Celsius temperature?"))
fahrenheit = 9/5 * celsius + 32
print("The temperature is ", fahrenheit, "degrees Fahrenheit.")
for i in range(5):
convert()
5.
def main():
celsius = 0
for i in range(11):
fahrenheit = 9/5 * celsius + 32
print("Celsius", celsius, "equals to Fahrenheit", fahrenheit, ".")
celsius += 10
main()
7.
def main():
print("This program calculates the future value.")
year = eval(input("How may years do you want?"))
principal = eval(input("How much the principal each year?"))
apr = eval(input("Enter the annual interest rate:"))
value = principal
for i in range(year):
value = (1 + apr) * value
value += principal
print("The value in", year, "years is: ", value)
main()
12.
def main():
print("This program calculates the expression's result.")
for i in range(100):
expr = eval(input("Enter an expression:"))
print(expr)
main()
chapter3
10.
import math
def main():
print("This program calculates the length of ladder.")
angle = eval(input("Enter the angle:")) # degree
radians = angle * math.pi / 180 # radians
height = 10
length = height / math.sin(radians)
print("The length of ladder is", length, ".")
main()
11.
def main():
print("This program calculates the sum of n natural numbers.")
num = eval(input("Please enter the number:"))
s = 0
for i in range(num + 1):
s += i
print("The sum is", s, ".")
main()
15.
import math
def main():
num = eval(input("Please input the number:"))
s = 0
for n in range(num):
s = s + (-1)**(n) * 4/(2 * n + 1)
error = math.pi - s
print("The sum is:", s, ".")
print("The error is,", error, ".")
main()