Python写経会2-1


# じゃんけんゲームを作りましょう


```python
import random
```


```python
?random.uniform()
```


```python
random.uniform(0,3)
```




    0.5907769638334399




```python
int(random.uniform(0,3))
```




    2




```python
res=[]
for i in range(10):
    res.append(int(random.uniform(0,3)))
```


```python
res
```




    [1, 0, 2, 2, 0, 2, 1, 1, 0, 1]




```python
you = input()
```

    2



```python
you
```




    '2'




```python
you = int(you)
```


```python
computer = int(random.uniform(0,3))
if you == computer:
    print("you: " + str(you))
    print("computer: " + str(computer))
    print("AIKO")
elif (you==0 and computer==1) or (you==1 and computer==2) or (you==2 and computer==0):
    print("you: " + str(you))
    print("computer: " + str(computer))
    print("WIN")
else:
    print("you: " + str(you))
    print("computer: " + str(computer))
    print("AIKO")
```

    you: 2
    computer: 0
    WIN


# break, continue


```python
for i in range(15):
    if i == 10: 
        break;
    print(i)    
```

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9



```python
for i in range(15):
    if i <= 7:
        continue
    print(i)
```

    8
    9
    10
    11
    12
    13
    14



```python
i = 0;
while (i < 100):
    i += 1;
    if i == 10: 
        break;
    print(i) 
```

    1
    2
    3
    4
    5
    6
    7
    8
    9


# 完成版1


```python
def janken():
    def get():
        you = int(input())
        return(you)
    while (True):
        you = get()
        computer = int(random.uniform(0,3))
        
        if you == computer:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("AIKO")
        elif (you==0 and computer==1) or (you==1 and computer==2) or (you==2 and computer==0):
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("WIN")
            break;
        else:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("LOSE")
            break;      
```


```python
janken()
```

    3
    you: 3
    computer: 0
    LOSE


# 変な入力もできるよね


```python
janken()
```

    23
    you: 23
    computer: 2
    LOSE



```python
janken()
```

    hkj



    ---------------------------------------------------------------------------

    ValueError                                Traceback (most recent call last)

    <ipython-input-42-da87b5bab661> in <module>()
    ----> 1 janken()
    

    <ipython-input-39-d81907d4668e> in janken()
          4         return(you)
          5     while (True):
    ----> 6         you = get()
          7         computer = int(random.uniform(0,3))
          8 


    <ipython-input-39-d81907d4668e> in get()
          1 def janken():
          2     def get():
    ----> 3         you = int(input())
          4         return(you)
          5     while (True):


    ValueError: invalid literal for int() with base 10: 'hkj'



```python
def janken():
    def get():
        print("Please input your choice number:\n0:グー、1:チョキ、2:パー")
        temp = input()
        while (temp not in ["0", "1", "2"]):
            print("Please input 0 or 1 or 2")
            temp = input()
        you = int(temp)
        
        return(you)
    while (True):
        you = get()
        computer = int(random.uniform(0,3))
        if you == computer:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("AIKO")
        elif (you==0 and computer==1) or (you==1 and computer==2) or (you==2 and computer==0):
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("WIN")
            break;
        else:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("LOSE")
            break;      
```


```python
janken()
```

    Please input your choice number:
    0:グー、1:チョキ、2:パー
    990
    Please input 0 or 1 or 2
    1
    you: 1
    computer: 1
    AIKO
    Please input your choice number:
    0:グー、1:チョキ、2:パー
    1
    you: 1
    computer: 2
    WIN


# 完成版２


```python
import random
import time
def janken():
    print("Welcome to janken game\n")
    def get():
        print("Please input your choice number:\n0:グー、1:チョキ、2:パー")
        time.sleep(0.2)
        temp = input()
        while (temp not in ["0", "1", "2"]):
            print("Please input 0 or 1 or 2")
            temp = input()
        you = int(temp)
        
        return(you)
    while (True):
        you = get()
        computer = int(random.uniform(0,3))
        if you == computer:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("AIKO")
        elif (you==0 and computer==1) or (you==1 and computer==2) or (you==2 and computer==0):
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("YOU WIN")
            break;
        else:
            print("you: " + str(you))
            print("computer: " + str(computer))
            print("YOU LOSE")
            break;   
```


```python
janken()
```

    Welcome to janken game
    
    Please input your choice number:
    0:グー、1:チョキ、2:パー
    2
    you: 2
    computer: 2
    AIKO
    Please input your choice number:
    0:グー、1:チョキ、2:パー
    1
    you: 1
    computer: 0
    YOU LOSE


# 完成版3 (forを使う)


```python
import random
import time
def janken():
    print("*"*30)
    print("Welcome to janken game")
    print("*"*30)
    def get():
        print("Please input your choice number:\n0:グー、1:チョキ、2:パー")
        time.sleep(0.2)
        for i in range(200):
            temp = input()
            if (temp in ["0", "1", "2"]):
                return(int(temp))
            else:
                print("Please input your choice number:\n0:グー、1:チョキ、2:パー")

            

    for i in range(100):
        you = get()
        computer = int(random.uniform(0,3))
        if you == computer:
            print("you: "+str(you))
            print("computer: "+str(computer))
            print("AIKO")
        elif (you==0 and computer==1) or (you==1 and computer==2) or (you==2 and computer==0):
            print("you: "+str(you))
            print("computer: "+str(computer))
            print("YOU WIN")
            break;
        else:
            print("you: "+str(you))
            print("computer: "+str(computer))
            print("YOU LOSE")
            break;
    print("*"*30)
    print("GAME OVER")
    print("*"*30)
```


```python
janken()
```

    ******************************
    Welcome to janken game
    ******************************
    Please input your choice number:
    0:グー、1:チョキ、2:パー
    2
    you: 2
    computer: 0
    YOU WIN
    ******************************
    GAME OVER
    ******************************