I will participate in an in-house study session on the theme of "Let's make a Web system in a language that I have never touched". Since I was in charge of python research, this is the second installment to summarize the research. The first half is mainly an excerpt from wikipedia.
notation
Data type
Object-orientation
I rewrote my eldest daughter's math problem script that I wrote in Ruby earlier in python. The impression I wrote is as follows. (I haven't done much, so it's not a big impression, but ... I'll add it as needed.)
keisan.rb
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
question_num = 10
score = 0
i = 0
while i < question_num do
val1 = rand(10)
val2 = rand(10)
print "(#{i + 1}) "
case rand(2)
when 0
print "#{val1} + #{val2} = "
correct_ans = val1 + val2
when 1
if val1 >= val2
print "#{val1} - #{val2} = "
correct_ans = val1 - val2
else
print "#{val2} - #{val1} = "
correct_ans = val2 - val1
end
end
ans = gets.to_i
if correct_ans == ans
p "Qinghai wave#{correct_ans}"
p "You did it! !!"
score += 1
else
p "Qinghai wave#{correct_ans}"
p "I'm sorry! !!"
end
i = i + 1
end
p "Your tensu#{((score.to_f / question_num.to_f) * 100).to_i}Ten! !!"
keisan.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import random
question_num = 10
score = 0
i = 0
while i < question_num:
val1 = random.randint(0,9)
val2 = random.randint(0,9)
operator_flg = random.randint(0,1)
print("(" + str(i + 1) + ") ", end='')
if operator_flg == 0:
formula = str(val1) + " + " + str(val2) + " = "
correct_ans = val1 + val2
elif operator_flg == 1:
if val1 >= val2:
formula = str(val1) + " - " + str(val2) + " = "
correct_ans = val1 - val2
else:
formula = str(val2) + " - " + str(val1) + " = "
correct_ans = val2 - val1
ans = input(formula)
if correct_ans == int(ans):
print("Qinghai wave" + str(correct_ans))
print("You did it! !!")
score += 1
else:
print("Qinghai wave" + str(correct_ans))
print("I'm sorry! !!")
i += 1
print("Your tensu" + str(int(float(score) / float(question_num) * 100)) + "Ten! !!")
Recommended Posts