-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbernerd.py
162 lines (95 loc) · 3.18 KB
/
numbernerd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# coding: utf-8
# Number Nerd
#
# Game to test knowledge of numbers.
# Code based on:
# http://www.nzqa.govt.nz/assets/qualifications-and-standards/qualifications/ncea/NCEA-subject-resources/Digital-Technologies/91076/91076-EXP-Student2-001.pdf
#
# ncea level one
# In[119]:
import random
import getpass
import giphypop
#from IPython.display import Image
#from IPython.core.display import HTML
import webbrowser
# Instead of asking for name use getpass and get the username that the user is loged in as.
# In[120]:
#get username
#username = input('What is your name? ')
theusr = getpass.getuser()
print('''Hello {}, welcome to this game of Number Nerd.
In this game you will learn to work out which of given set of numbers is the biggest,
or which is the smallest.'''.format(theusr))
# In[121]:
score = 0
# Generate two random number that are not the same. Instead of using randint use sample instead.
#
# Thanks stackoverflow:
#
# http://stackoverflow.com/questions/22842289/python-generate-n-unique-random-numbers-within-a-range
# In[122]:
ransam = random.sample(range(1,75), 2)
# Random choose whether to ask the user to pick the smallest number or the biggest number, then ask the user these questions until they get 5 points.
# In[123]:
randchoic = random.randint(0,1)
# In[124]:
smallnum = min(ransam)
# In[125]:
largenum = max(ransam)
# In[126]:
largenum
# In[ ]:
g = giphypop.Giphy()
winres = [x for x in g.search('win')]
wingifz = winres[random.randint(0, len(winres))]
# In[ ]:
failres = [x for x in g.search('fail')]
failgif = winres[random.randint(0, len(failres))]
# In[ ]:
wingifz.media_url
# In[ ]:
failgif.media_url
# Not happy with the if inside a if, plus repeating code with only change being 0/1 and small/bigger. Merge these?
# In[ ]:
if randchoic == 0:
while True:
try:
answer = int(input("Of {} and {}, which number is smaller? ".format(ransam[0], ransam[1])))
break
except ValueError:
print('Please type in one of the given numbers as your answer')
if answer == smallnum:
score += 1
print('Well done {}! You now have {} points. '.format(theusr, score))
#Image(url= wingifz.media_url)
webbrowser.open_new(wingifz.media_url)
print(wingifz.media_url)
else:
print('That is wrong!')
#Image(url= failgif.media_url)
print(failgif.media_url)
webbrowser.open_new(failgif.media_url)
# In[ ]:
failgif.media_url
# In[ ]:
if randchoic == 1:
while True:
try:
answer = int(input("Of {} and {}, which number is biggest? ".format(ransam[0], ransam[1])))
break
except ValueError:
print('Please type in one of the given numbers as your answer')
if answer == largenum:
score += 1
print('Well done {}! You now have {} points. '.format(theusr, score))
#Image(url= wingifz.media_url)
webbrowser.open_new(wingifz.media_url)
else:
#Image(url= failgif.media_url)
print('That is wrong!')
webbrowser.open_new(failgif.media_url)
# In[ ]:
#Image(url=wingifz.media_url)
# open web browser to image
# In[ ]: