First python ② Try to write code while examining the features of python

background

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.

What is python

Features of python

notation

Data type

Object-orientation

Let's compare

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=&#39;&#39;)

  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

First python ② Try to write code while examining the features of python
I wrote the code to write the code of Brainf * ck in python
About the features of Python
Try to get the function list of Python> os package
Try to automate the operation of network devices with Python
I want to know the features of Python and pip
Try to extract the features of the sensor data with CNN
First Python 3 ~ The beginning of repetition ~
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Python] How to get the first and last days of the month
[Python3] Rewrite the code object of the function
[Python] Try pydash of the Python version of lodash
Python amateurs try to summarize the list ①
[Python] Get the character code of the file
Try to estimate the parameters of the gamma distribution while simply implementing MCMC
Try to image the elevation data of the Geographical Survey Institute with Python
I tried to get the authentication code of Qiita API with Python.
Try to write python code to generate go code --Try porting JSON-to-Go and so on
Python code to determine the monthly signal of a relative strength investment
I found out by making a python script to record radiko while reading the code of the predecessors
"Cython" tutorial to make Python explosive: When C ++ code depends on the library. First of all, CMake.
First steps to try Google CloudVision in Python
[Python] Read the source code of Bottle Part 2
Try the python version of emacs-org parser orgparse
Try to solve the Python class inheritance problem
Try to solve the man-machine chart with Python
[Python] Read the source code of Bottle Part 1
Try the free version of Progate [Python I]
The trick to write flatten concisely in python
Try CIing the pushed python code on GitHub.
I touched some of the new features of Python 3.8 ①
Try using the collections module (ChainMap) of python3
Code for checking the operation of Python Matplotlib
List of Python code to move and remember
Convert the character code of the file with Python3
Try to simulate the movement of the solar system
Try posting to Qiita for the first time
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Note: How to get the last day of the month with python (added the first day of the month)
I tried to refactor the code of Python beginner (junior high school student)
I want to write in Python! (1) Code format check
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
The wall of changing the Django service from Python 2.7 to Python 3
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
Template of python script to read the contents of the file
How to get the number of digits in Python
[Python] Try to read the cool answer to the FizzBuzz problem
The first step of machine learning ~ For those who want to implement with python ~
Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
Try to solve the internship assignment problem with Python
Try touching the micro: bit with VS Code + Python
The first algorithm to learn with Python: FizzBuzz problem
Rewrite Python2 code to Python3 (2to3)
[python] option to turn off the output of click.progressbar
Let's break down the basics of TensorFlow Python code
How to write a list / dictionary type of Python3
the zen of Python
Get the return code of the Python script from bat
Try to estimate the number of likes on Twitter