Multiplication program - python-2.7

I am making this multiplication program in python 2.7.5 for my sister, and I don't know how to count the correct answers. Here is the code:
import easygui
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
answer = easygui.enterbox("What is " + str(i) + ' times 8?')
if int(answer) == i * 8:
easygui.msgbox("That is correct!")
else:
easygui.msgbox("Wrong!")

Why not just add a variable to keep count for you?
import easygui
correct_answers = 0 # start with none correct
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
answer = easygui.enterbox("What is " + str(i) + ' times 8?')
if int(answer) == i * 8:
easygui.msgbox("That is correct!")
correct_answers += 1 # increment
else:
easygui.msgbox("Wrong!")
You could improve your program by making the base number a variable, too, and using Python's str.format() rather than addition:
base = 8
...
answer = easygui.enterbox("What is {0} times {1}?".format(i, base))
if int(answer) == i * base:
...

Related

Find index of item in list where sum of start of list to index is greater than X

I am looking for a fast implementation of the following code; using, for instance, map() or next():
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
total_so_far = 0
for i in l:
total_so_far += i
if total_so_far > 14:
break
print(i)
The code prints the index of item in list where sum of start of list to the index is greater greater than 14.
Note: I need to continuously update the link in another loop. Therefore, a solution in numpy would probably be too slow, because it cannot update a list in-place.
You can also make use of itertools.accumulate() together with enumerate() and next():
In [1]: from itertools import takewhile
In [2]: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: next(index for index, value in enumerate(accumulate(l)) if value > 14)
Out[3]: 5

How to switch values in a list to print an alternated version of the list?

I'm having some trouble figuring out how to switch numbers in a long list.
For example if were to have a list:
numbers = [1,2,3,4,5,6,7,8]
and wanted to instead print it in the form of:
numbers_2 = [2,1,4,3,6,5,8,7]
such that each pair would be switched, using a for-loop. I thought about doing something like:
for i in range(0, len(numbers), 2):
But wasn't really able to get much further.
Loop every second index and swap two adjacent items:
numbers = [1,2,3,4,5,6,7,8]
for i in range(1, len(numbers), 2):
numbers[i-1], numbers[i] = numbers[i], numbers[i-1]
Not sure about the other answers, but this one will also work with a list of an uneven length, and leave the last item untouched.
Take 2 halves, and rearrange them:
numbers = [1,2,3,4,5,6,7,8]
first = numbers[::2]
second = numbers[1::2]
numbers_2 = sum(map(list, zip(second, first)), [])
Try this:
def swap_array_elements (a):
for i in range (0, len(a) - 1, 2):
a[i], a[i +1] = a[i + 1], a[i]
return a
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print (swap_array_elements (a))
# prints: [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]

Why does my variable's value is changed by a line that doens't touch it in Python? [duplicate]

This question already has answers here:
Why does my original list change? [duplicate]
(2 answers)
Closed 6 years ago.
The answer to my question seems pretty straightforward : it can't so it doesn't. Yet I believe it is happening to me and it is driving me fairly crazy. Therefore I would very much appreciate your opinion.
Here's the situation. I'm writing this script which has the following function in it :
def ReduceReferenceCode():
Code = ReferenceCode
if E == 7:
CritLimit = 2
elif E == 4:
CritLimit = 1
if D < CritLimit:
for i in [4, 5, 6]:
if Code[i] >= CritLimit:
print ReferenceCode
Code[i] = Code[i] - CritLimit
print ReferenceCode
break
else:
Code[7] = Code[7] - CritLimit
Code[9] = 1
return Code
The value of my ReferenceCode variable - which is passed as an argument to the program with sys.argv - is changed between the two print commands. My main function prints both ReferenceCode and my reduced code for comparison purposes, which is the value stored in Code
Here's the program's output:
[1, 4, 3, 4, 9, 7, 2, 0, 6, 7, 9, 2]
[1, 4, 3, 4, 7, 7, 2, 0, 6, 7, 9, 2]
The reference code is [1, 4, 3, 4, 7, 7, 2, 0, 6, 1, 9, 2] and the reduced reference code is [1, 4, 3, 4, 7, 7, 2, 0, 6, 1, 9, 2]
Both variables should not have the same value and I really don't see why the operation on Code[i] is affecting ReferenceCode's value.
Any insight would be highly appreciated :-)
The value of your ReferenceCode is a list. That list has another reference called Code. Updating the list from either of these references changes the one-any-only list object. The fix is to copy the list
Code = ReferenceCode[:]
You did that :
Code = ReferenceCode
So both references are pointing to the same object.

Using map to make a list from a range

I want to create a list of items from a range using the map command. I have the following code that's supposed to take the range between two numbers (firstnum and secondnum) and a lambda statement that says to increment between the two numbers and create myList of the results (between the two ends of the range). However my syntax is wrong, not sure why...
["myList"] = map(lambda x, y: x + 1, range(firstnum..secondnum))
This would be valid syntax:
>>> list(map(lambda x: x + 1, range(2, 12)))
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
But you can get the same result much simpler:
>>> list(range(3, 13))
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Or more general:
>>> firstnum = 2
>>> secondnum = 12
>>> (list(map(lambda x: x + 1, range(firstnum, secondnum))) ==
list(range(firstnum + 1, secondnum + 1)))
True

Remove adjacent element in a list in python

I am trying to do a simple python program that removes all the adjacent elements in a list
def main():
a = [1, 5, 2, 3, 3, 1, 2, 3, 5, 6]
c = len(a)
for i in range (0, c-2):
if a[i] == a[i+1]:
del a[i]
c = len(a)
print a
if __name__ == '__main__':
main()
and the output is
[1, 5, 2, 3, 3, 2, 3, 5, 6] which is all fine!
If change the a list to a = [1, 5, 2, 3, 3, 1, 2, 2, 5, 6]
then it gives an error
index list out of range
**if a[i] == a[i+1]**
It shouldn't be complaining about the index out of range as I am calculating the len(a) every time it deletes an element in the list. What am I missing here?
for i in range (0, c-2):
This is not like a for loop in some other languages; it’s iterating over a list returned (once) by range. When you change c later, it does not affect this loop.
You can use while instead:
c = len(a)
while i < c - 2:
if a[i] == a[i + 1]:
del a[i]
c = len(a)
else:
i += 1
There’s also itertools.groupby:
import itertools
def remove_consecutive(l):
return (k for k, v in itertools.groupby(l))
Here's a slightly different approach:
origlist=[1, 5, 2, 3, 3, 1, 2, 3, 5, 6]
newlist=[origlist[0]]
for elem in origlist[1:]:
if (elem != newlist[-1]):
newlist.append(elem)
The itertools answer above may be preferred, though, for brevity and clarity...