[Tutor] guess my number game

2018-05-08 Thread Kerri Murphy
My students are creating a guess my number game.

They are trying to take this type of code (the flow of it), and turn it
into a code using a while loop.
Here is the first code

n = int(input('Guess my number: '))if (n <= 172 and n >= 174):
print('Correct')elif (n >= 174):
a = int(input('Go Down'))
if (a >= 174):
c = int(input('Go Down'))
if (c >= 174):
g = int(input('Last Guess Go Down'))
if (g == 173):
print('Correct')
elif (c <= 172):
i = int(input('Last Guess Go Up'))
if (i == 173):
print('Correct')
else:
print('Correct')
elif (a <= 172):
e = int(input('Go up'))
if (e >= 174):
f = int(input('Last Guess Go Down'))
if (f == 173):
print('Correct')
elif (e <= 172):
h = int(input('Last Guess Go Up'))
if (h == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')elif (n <= 172):
b = int(input('Go Up'))
if (b >= 174):
d = int(input('Go Down'))
if (d >= 174):
j = int(input('Last Guess Go Down'))
if (j == 173):
print('Correct')
elif (d <= 172):
m = int(input('Last Guess Go Up'))
if (m == 173):
print('Correct')
else:
print('Correct')
elif (b <= 172):
e = int(input('Go Up'))
if (e >= 174):
k = int(input('Last Guess Go Down'))
if (k == 173):
print('Correct')
elif (e <= 172):
l = int(input('Last Guess Go Up'))
if (l == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')else:
print('Correct')



Here is the code with the while loop
import random
n = (random.randint(0,100))
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = 0
while (c < 10):
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = c + 1
if (g >= n):
print('Lower!')
elif (g <= n):
print('Higher!')
elif (g == n):
break
if (g == n):
print('You guess my number! It took you ' + str(c) + ' tries!')


Everyone's code just keeps asking for numbers without giving feedback,
except for the longer code above.

Can you help us consolidate the code?  We are using random.randint.

Thank you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] guess my number game

2018-05-08 Thread Jan Erik Moström



Here is the code with the while loop
import random
n = (random.randint(0,100))
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = 0
while (c < 10):
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = c + 1
if (g >= n):
print('Lower!')
elif (g <= n):
print('Higher!')
elif (g == n):
break
if (g == n):
print('You guess my number! It took you ' + str(c) + ' tries!')


Everyone's code just keeps asking for numbers without giving feedback,
except for the longer code above.


For me it prints out Lower/higher but not for the first time since the 
code asks a second time before doing anything with the reply.


Here is a modified version where I kept most of the structure you have. 
Note that '>=' and '<=' was changed to '>' and '<'.


import random
n = (random.randint(0,100))
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = 0
while (c < 10 and g != n):
if (g > n):
print('Lower!')
elif (g < n):
print('Higher!')
c = c + 1
g = int(input('Guess my number, 0 to 100, you have {} 
left'.format(10-c)))


if (g == n):
print('You guess my number! It took you ' + str(c) + ' tries!')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] guess my number game

2018-05-08 Thread Mats Wichmann
On 05/08/2018 06:04 AM, Jan Erik Moström wrote:
> 
>> Here is the code with the while loop
>> import random
>> n = (random.randint(0,100))
>> g = int(input('Guess my number, 0 to 100, you have 10 chances'))

you should get your students into good programming habits right away.
Here you are taking data from outside the program's own control, and in
the current terminology that data is "tainted" until you validate it -
there should be a check that you got back a number in the range that you
specify.  Yes in this simple example you don't do anything with the
guess that could be harmful, but they should still be learning to keep
such considerations in mind.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to separate UI code from program logic?

2018-05-08 Thread boB Stepp
On Mon, May 7, 2018 at 12:26 AM, boB Stepp  wrote:

> def get_collatz_number(integer):
> """Returns the Collatz sequence number corresponding to integer.  integer
> must be > 0, or the sequence will not converge to 1."""
>
> if integer % 2 == 0:
> return integer // 2
> else:
> return 3 * integer + 1
>
> def generate_collatz_sequence(seed):
> """Creates a generator, which will yield a Collatz sequence starting from
> seed.  seed must be a positive integer, or the sequence will not converge 
> to
> 1."""
>
> collatz_number = seed
> while True:
> collatz_number = get_collatz_number(collatz_number)
> yield collatz_number
> if collatz_number == 1:
> return
> Questions and comments:

After taking a break from this and coming at this afresh, I
immediately saw an answer to this question:

> 2)  I spent a lot of effort trying to come up with a way to combine
> the two functions, get_collatz_number() and
> generate_collatz_sequence(), into something both more compact and more
> readable, but I was unsuccessful.  I suspect there is a better way.
> Is there?  And how would I do it?

def generate_collatz_sequence(seed):
"""Creates a generator, which will yield a Collatz sequence starting from
seed.  seed must be a positive integer, or the sequence will not converge to
1."""

collatz_number = seed
while True:
if collatz_number % 2 == 0:
collatz_number //= 2
else:
collatz_number = 3 * collatz_number + 1
yield collatz_number
if collatz_number == 1:
return

Judging from the lack of responses, I guess I must have been on track
on the other questions.

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread Brad M
Hi all:

I am trying out some c based module in a .dll file on windows.

// helloworld.c
#include 

__declspec(dllexport) void helloworld()
{
printf("Hello Everyone!!!");
}

I compile this by typing this in the command line:

cl /LD /I C:\python\include helloworld.c C:\python\libs\python36.lib

I get helloworld.dll for the above.



and the following is the python caller

import ctypes
mydll = ctypes.CDLL('helloworld')
mydll.helloworld()



However, this doesn't print anything on the python window.
What I would like is to do is to be able to use printf() in my .dll
by having the c code pop up a console window to print or
to have something that can print() in the python window somehow.

Is this possible?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread Alan Gauld via Tutor
I'm guessing at the answer here because I'm on vacation with no access to a pc 
of any kind let alone Windows. But are you running python inside an ide? If so 
you might find you get the expected result if you use a command prompt, since 
printf usually sends output to stdout.

This is another example of the rule that you should never mix display and 
logic. If the dll returned the string instead of printing it you could display 
it from python easily, but by using print you make your code dependant on a 
stdout being visible.

Hth

Alan g


On 8 May 2018, at 23:03, Brad M  wrote:

Hi all:

I am trying out some c based module in a .dll file on windows.

// helloworld.c
#include 

__declspec(dllexport) void helloworld()
{
printf("Hello Everyone!!!");
}

I compile this by typing this in the command line:

cl /LD /I C:\python\include helloworld.c C:\python\libs\python36.lib

I get helloworld.dll for the above.



and the following is the python caller

import ctypes
mydll = ctypes.CDLL('helloworld')
mydll.helloworld()



However, this doesn't print anything on the python window.
What I would like is to do is to be able to use printf() in my .dll
by having the c code pop up a console window to print or
to have something that can print() in the python window somehow.

Is this possible?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to separate UI code from program logic?

2018-05-08 Thread Alan Gauld via Tutor
Bob,

On 8 May 2018, at 19:56, boB Stepp <
>Judging from the lack of responses, I guess I must have been on track
>on the other questions.

On The basics yes. There were a few picky details I would normally have 
mentioned but I'm on vacation and replying inline via gmail on my tablet is 
just too painful! 😉

Alan g
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] guess my number game

2018-05-08 Thread Alan Gauld via Tutor
The first block of code is full of errors and couldn't work so I have no idea 
what you were really doing!

 The second block should kind of work. From your description I'd guess you have 
an indentation error such that most of the code that should be inside the loop 
is being bypassed. Are you sure the code you sent us exactly what you are 
running?

Alan g

On 8 May 2018, at 10:39, Kerri Murphy  wrote:

My students are creating a guess my number game.

They are trying to take this type of code (the flow of it), and turn it
into a code using a while loop.
Here is the first code

n = int(input('Guess my number: '))if (n <= 172 and n >= 174):
print('Correct')elif (n >= 174):
a = int(input('Go Down'))
if (a >= 174):
c = int(input('Go Down'))
if (c >= 174):
g = int(input('Last Guess Go Down'))
if (g == 173):
print('Correct')
elif (c <= 172):
i = int(input('Last Guess Go Up'))
if (i == 173):
print('Correct')
else:
print('Correct')
elif (a <= 172):
e = int(input('Go up'))
if (e >= 174):
f = int(input('Last Guess Go Down'))
if (f == 173):
print('Correct')
elif (e <= 172):
h = int(input('Last Guess Go Up'))
if (h == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')elif (n <= 172):
b = int(input('Go Up'))
if (b >= 174):
d = int(input('Go Down'))
if (d >= 174):
j = int(input('Last Guess Go Down'))
if (j == 173):
print('Correct')
elif (d <= 172):
m = int(input('Last Guess Go Up'))
if (m == 173):
print('Correct')
else:
print('Correct')
elif (b <= 172):
e = int(input('Go Up'))
if (e >= 174):
k = int(input('Last Guess Go Down'))
if (k == 173):
print('Correct')
elif (e <= 172):
l = int(input('Last Guess Go Up'))
if (l == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')else:
print('Correct')



Here is the code with the while loop
import random
n = (random.randint(0,100))
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = 0
while (c < 10):
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = c + 1
if (g >= n):
print('Lower!')
elif (g <= n):
print('Higher!')
elif (g == n):
break
if (g == n):
print('You guess my number! It took you ' + str(c) + ' tries!')


Everyone's code just keeps asking for numbers without giving feedback,
except for the longer code above.

Can you help us consolidate the code?  We are using random.randint.

Thank you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Return keys with unique values from a dict

2018-05-08 Thread Glen
Hello,

I had a task to define a function that would take a dict input and return
only the keys with unique values into a list.

Here is my code, which was awarded full marks, but I am pretty unhappy with
it. I feel like I did a band-aid job after my first submission did pass
mall the unit tests. I'd appreciate any advice on a better way to do this
if anyone has some extra time.

Thanks,

def uniqueValues(theDict):
'''
theDict: a dictionary
'''
ans = []
rev = {}
toDel = []
for key, value in aDict.items():
if value not in rev:
rev[value] = key
elif value in rev:
toDel.append(value)

for item in toDel:
try:
del rev[item]
except:
pass

for item in rev:
ans.append(rev[item])

return sorted(ans)



Virus-free.
www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return keys with unique values from a dict

2018-05-08 Thread Mats Wichmann
On 05/08/2018 04:33 PM, Glen wrote:
> Hello,
> 
> I had a task to define a function that would take a dict input and return
> only the keys with unique values into a list.
> 
> Here is my code, which was awarded full marks, but I am pretty unhappy with
> it. I feel like I did a band-aid job after my first submission did pass
> mall the unit tests. I'd appreciate any advice on a better way to do this
> if anyone has some extra time.
> 
> Thanks,
> 
> def uniqueValues(theDict):
> '''
> theDict: a dictionary
> '''
> ans = []
> rev = {}
> toDel = []
> for key, value in aDict.items():
> if value not in rev:
> rev[value] = key
> elif value in rev:
> toDel.append(value)
> 
> for item in toDel:
> try:
> del rev[item]
> except:
> pass
> 
> for item in rev:
> ans.append(rev[item])
> 
> return sorted(ans)

There are many ways to tackle this - if you get six answers you'll
probably get six different ones :) You're already using the trick of
flipping, or reversing the dictionary, so we can maybe tweak that a
little bit - this is not the only way, you could use try/except instead
of get() to handle the case of the not-yet-added key:

for key, value in theDict.items():
rev[value] = rev.get(value, [])
rev[value].append(key)

Having reversed the dict, you can build your answer list on the fly by
knowing that value lists (of keys from the original) which have a size
of 1 are "unique", if the (new) key has multiple values, then those were
not unique. You then pull out the value from that list and add it to
your answer list.

ans = [value[0] for key, value in rev.items() if len(value) == 1]

Whether that's "better" than your answer is up for discussion, but
something to think about.

The choice of answers may also depend a bit on the character of the data.






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread eryk sun
On Tue, May 8, 2018 at 9:39 AM, Brad M  wrote:
>
> I compile this by typing this in the command line:
> cl /LD /I C:\python\include helloworld.c C:\python\libs\python36.lib

You're not using Python's C API, so you only need `cl /LD helloworld.c`.

> However, this doesn't print anything on the python window.
> What I would like is to do is to be able to use printf() in my .dll
> by having the c code pop up a console window to print or
> to have something that can print() in the python window somehow.

By Python window, do you mean the IDLE GUI? If the library is loaded
in a GUI program in which stdout is invalid, it will have to manually
allocate a console via `AllocConsole` and open the screen buffer using
the reserved filename "CONOUT$". Then it can print to the opened FILE
stream using fprintf(). But I'll reiterate Alan here that this would
be unusual behavior for a shared library, unless it's specifically
intended as a UI library.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread eryk sun
On Wed, May 9, 2018 at 1:30 AM, Brad M  wrote:
>
> If you want to know where your program went when something went wrong or
> when it triggers a if condition, how do you do it?

Look into use the logging module [1] and debuggers [2], either
dedicated like Winpdb or in an IDE such as PyCharm.

[1]: https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
[2]: https://wiki.python.org/moin/PythonDebuggingTools

For debugging a DLL, learn to use a native debugger, such as
Microsoft's WinDbg, cdb, or the Visual Studio debugger.
OutputDebugString [3] writes a string to an attached debugger.

[3]: https://msdn.microsoft.com/en-us/library/aa363362
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor