0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python boot camp by Dr.Angela day5

Last updated at Posted at 2021-02-09

#Loop / for 構文
pythonにおいてIndentationが非常に大事なことはよく知られている。

Ikemen = ["Kamiki","Suda"]
print(Ikemen)

for Ikeman in Ikemen:
  print(Ikeman)
  print(Ikeman + "kun")

#出力結果
['Kamiki','Suda']
Kamiki 
Kamiki kun
Suda 
Suda kun

##Mission>> Average Height
・sum()とlen()は使用不可

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

total = 0
for x in student_heights:
  total +=x
print(f"{total}cm")

number = 0
for y in student_heights:
  number +=1

average =total/number
print(f"{round(average)cm}")

#max() / min()

student_scores = input("Input a list of student scores ").split()
for n in range(0,len(student_scores)):
  student_scores[n] = int(student_scores[n])
print(student_scores)

for score in student_scores:
  if score > height_score:
  highest_score=score
print(f"The highest score in the class is: {highest_score}")

max_score =max(student_scores)
min_score =min(student_scores)
print(f"The highest score in the class is: {max_score}")
print(f"The lowest score in the class is: {min_score}")

#Mission>> FizzBuzz!!
もっともよく出される問題ではないか?と思う。
3,5,15の割る数のうち最も大きな数を最初に持ってくることがポイント。
3や5で割り切れるものが先に読み込まれてしまうと、15に引っ掛けることができないので、値の大きいものからチェックする必要がある。今回は3と5は互いに素であるから簡単であるが、2と4や8と14などとなると、包含関係が出てきて、条件分岐がかなり複雑になるので注意。

for x in range(1,101):
  if x %15 ==0:
    print("FizzBuzz!!")
  elif x %5 ==0:
    print("Buzz!")
  elif x %3 ==0:
    print("Fizz!")
  else:
    print(x)
print("Finish!!")

#出力結果
1
2
Fizz!
4
Buzz!
Fizz!
7
8
Fizz!
...
以下省略
...
FizzBuzz!!

#Mission>> Password generator
・指定条件がなかったため、.join()と.choices(a,k=個数)使用
・ .join()を使用して各々のlistで1つのlistにして、最後にそれぞれのlistを合体
・ 空リストにそれぞれの要素をfor文で回して追加し、リストを1つにまとめた
→ listから複数の要素をrandomで取り出す方法が分からなかったのでググったところ、.choice(a,k=個数)が使えそうだったので採用

#example1
import random
letters =['a','b','c','d','e','f','A','B','C','D','E',]
numbers =['0','1','2','3','4','5','6','7','8','9']
symbols =['!','#','$','%','&','(',')','*','+']
print("Generate Password>> ")
nr_letters =int(input("How many letters would you like in your password?\n"))
nr_symbols =int(input(f"How many symbols would you like?\n"))
nr_numbers =int(input(f"How many numbers would you like?\n"))

s_letters =random.choices(letters,k=nr_letters)
s_symbols =random.choices(symbols,k=nr_symbols)
s_numbers =random.choices(numbers.k=nr_numbers)

password=""         #空文字列をつくっておく
for x in s_letters:
  password +=x
for y in s_symbols:
  password +=y
for z in s_numbers:
  password +=z
print(password)
#出力結果
How many letters would you like in your password?
5
How many symbols would you like?
3
How many numbers would you like?
1
(aB$&e5aAc

・選んだ要素を最終的に全てshuffleすることでpasswordを定める

#example2
#上部は一緒。途中からこちらに切り替える

s_letters =random.choices(letters,k=nr_letters)
s_symbols =random.choices(letters,k=nr_symbols)
s_numbers =random.choices(letters,k=nr_symbols)

password =[]        #空リストを作っておく
for x in s_letters:
  password +=x
for y in s_symbols:
  password +=y
for z in s_numbers:
  password +=z

random.shuffle(password)     #ここでリスト内シャッフル!
print("".join(password))     #シャフル後リストを結合し、文字列として出力

#出力結果
How many letters would you like in your password?
2
How many symbols would you like?
3
How many numbers would you like?
1
(aB$&e5

listの追加には.append()を使っても良い。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?