Re: [Tutor] Help with guess my number game

2014-10-13 Thread Alan Gauld

On 13/10/14 11:40, אופיר לירון wrote:


# set the initial values

the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
 if guess > the_number:
 print("Lower...")
 else:
 print("Higher...")
 guess = int(input("Take a guess: "))

 tries += 1
 if tries > 5:
 break


so far so good
almost...


 if guess != the_number:
 print ("you failed, the number was", the_number)


This is still inside the loop. You want to remove the
indentation so this only happens after you exit the loop.
Otherwise you tell the user the answer before they guess
it (or have 5 goes) and it doesn't work right if the
first guess is correct...


input("\n\nPress the enter key to exit.")


You need the if/else to look like this.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


[Tutor] Fwd: Re: Help with guess my number game

2014-10-13 Thread Danny Yoo
-- Forwarded message --
From: אופיר לירון 
Date: Mon, Oct 13, 2014 at 1:20 PM
Subject: Re: Re: [Tutor] Help with guess my number game
To: Danny Yoo 



Hi Danny

Thanks for your response,

I think it is part of the problem, but my intention was to end the
first loop only after 5 tries.

I don't understand why the code go to the print section after only two
gusses and not five. and it also reveal the_number before the game
end.

Again thank for your help.

Ofir


שולח: Danny Yoo,
נושא: Re: [Tutor] Help with guess my number game

>
> if guess != the_number:
>
> print ("you failed, the number was", the_number)
>
> elif guess==the_number:
>
> print("You guessed it! The number was", the_number)
>
> print("And it only took you", tries, "tries!n")


This block of code appears to be applied for every iteration through
your loop. Is that your intention?


Walla! Mail - Get your free unlimited mail today
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with guess my number game

2014-10-13 Thread Danny Yoo
>
> if guess != the_number:
>
> print ("you failed, the number was", the_number)
>
> elif guess==the_number:
>
> print("You guessed it!  The number was", the_number)
>
> print("And it only took you", tries, "tries!\n")


This block of code appears to be applied for every iteration through
your loop.  Is that your intention?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with guess my number game

2014-10-13 Thread אופיר לירון
Hi,I am new into Python, and using the bookPython Programming for the Absolute Beginner by Michael Dawson.One of the taks in chapte 3 is to change the "guess my number game" to include only 5 gusses and give appropriate messege at the end (in case no sucssesful guess was done).I have tried to add break for the loop after the variable "tries>5". however it seems that the program gets out from the loop after only 2 gusses. could someone give the reson or the solution to this issue.The cose is attached below:# Guess My Number## The computer picks a random number between 1 and 100# The player tries to guess it and the computer lets# the player know if the guess is too high, too low# or right on the moneyimport random  print("\tWelcome to 'Guess My Number'!")print("\nI'm thinking of a number between 1 and 100.")print("Try to guess it in as few attempts as possible.\n")# set the initial valuesthe_number = random.randint(1, 100)guess = int(input("Take a guess: "))tries = 1# guessing loopwhile guess != the_number:    if guess > the_number:        print("Lower...")    else:        print("Higher...")                guess = int(input("Take a guess: "))    tries += 1    if tries > 5:        break              if guess != the_number:        print ("you failed, the number was", the_number)    elif guess==the_number:        print("You guessed it!  The number was", the_number)        print("And it only took you", tries, "tries!\n")input("\n\nPress the enter key to exit.")Thanks for your helpofi...@walla.co.ilWalla! Mail - Get your free unlimited mail today___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Compare two text files

2014-10-13 Thread Steven D'Aprano
On Mon, Oct 13, 2014 at 03:54:59PM +0800, Crusier wrote:
> Hi Alan,
> 
> Attached are the two text files (stocklist.txt & stocklist1.txt) which I
> want to do a comparison with the content of the file, Basically, I want to
> see if there are any new numbers added to the file.
> 
> Please comment on the sequence of the file:
> 1. First, Open up the File No. 1 and put the string into a list.
> 2. Second, Open the File No. 2 and put the string into a list.
> 3. Use difflib to compare

Sounds like a good plan to me. Something like this would work:

py> import difflib
py> a = '1728.HK 1033.HK 2393.HK 0968.HK 3378.HK'.split()
py> b = '1728.HK 1036.HK 2393.HK 0968.HK 2784.HK 3378.HK'.split()
py> print '\n'.join(difflib.unified_diff(a, b))
---

+++

@@ -1,5 +1,6 @@

 1728.HK
-1033.HK
+1036.HK
 2393.HK
 0968.HK
+2784.HK
 3378.HK


Obviously you don't type in the strings '1728.HK...', you read them from 
the files you wish to compare.



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


Re: [Tutor] Compare two text files

2014-10-13 Thread Dave Angel
Crusier  Wrote in message:

>  
 Attached are the two text files (stocklist.txt & stocklist1.txt) which I want 
to do a comparison with the content of the file, Basically, I want to see if 
there are any new numbers added to the file. 

>  Please comment on the sequence of the file:
1. First, Open up the File No. 1 and put the string into a list. 
2. Second, Open the File No. 2 and put the string into a list. 
3. Use difflib to compare

I don't see what the included code had to do with the problem, 
 since difflib doesn’t care about numbers. It compares sequences
 (like lists) of strings.

So you have a couple of files to read in. Normally you might use
 readlines, but there don't seem to be any newlines in the files.
 So you'll need split, or something similar. 

Come back when you've made an attempt at the problem, and ask a
 question about your code. Otherwise you're just asking others to
 do your homework for you. That's no way to learn.

And please post in plain text. Html messages can cause lots of
 problems. 




-- 
DaveA

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


[Tutor] Compare two text files

2014-10-13 Thread Crusier
Hi Alan,

Attached are the two text files (stocklist.txt & stocklist1.txt) which I
want to do a comparison with the content of the file, Basically, I want to
see if there are any new numbers added to the file.

Please comment on the sequence of the file:
1. First, Open up the File No. 1 and put the string into a list.
2. Second, Open the File No. 2 and put the string into a list.
3. Use difflib to compare


This is some of the code I had written.

#Remove ".HK" from the stock list

def remove_HK():

f = open('stock_list.txt', "r")
output = open('stock_lista.txt', "w")

output.write(f.read().replace(".hk","").replace(".HK",""))

f.close()
output.close()

remove_HK()

My thinking is first remove ".HK" so I could compare in as int to another
text file.

Thank you
Henry
1728.HK 1033.HK 2393.HK 0968.HK 3378.HK 3049.HK 1661.HK 8269.HK 3393.HK 0151.HK 
0303.HK 0345.HK 0913.HK 0220.HK 0696.HK 0570.HK 3886.HK 2283.HK 3382.HK 0882.HK 
1065.HK 0826.HK 3823.HK 1613.HK 1228.HK 2382.HK 1089.HK 0981.HK 0598.HK 1099.HK 
0361.HK 1177.HK 0750.HK 0444.HK 0460.HK 2877.HK 2313.HK 0152.HK 0747.HK 2607.HK 
0563.HK 2727.HK 0205.HK 8047.HK 1004.HK 2010.HK 8201.HK 1345.HK 2328.HK 1515.HK 
8311.HK 0402.HK 1323.HK 8180.HK 0553.HK 1618.HK 0231.HK 2186.HK 1108.HK 8058.HK 
8237.HK 1212.HK 0381.HK 6136.HK 1638.HK 3336.HK 0419.HK 2211.HK 0923.HK 0438.HK 
0091.HK 0167.HK 1886.HK 1071.HK 0336.HK 2811.HK 6823.HK 8292.HK 0911.HK 0566.HK 
1367.HK 2208.HK 0283.HK 0530.HK 0175.HK 3800.HK 0451.HK 0500.HK 0038.HK 8123.HK 
8018.HK 3360.HK 0729.HK 1856.HK 1808.HK 1330.HK 0895.HK 1072.HK 2880.HK 3898.HK 
0080.HK 0867.HK 0471.HK 2722.HK 1060.HK 1313.HK 1333.HK 0728.HK 2198.HK 2380.HK 
0572.HK 1185.HK 0085.HK 0217.HK 0370.HK 0031.HK 1196.HK 2623.HK 0476.HK 1375.HK 
0996.HK 2324.HK 3188.HK 1848.HK 6828.HK 8321.HK 0285.HK 0154.HK 2357.HK 0232.HK 
0161.HK 1803.HK 0899.HK 2020.HK 1131.HK0471.HK 3800.HK 0728.HK 1033.HK 1099.HK 2357.HK 0566.HK 2328.HK 0232.HK 0729.HK 
2208.HK 0598.HK 2186.HK 0231.HK 0175.HK 0981.HK 0285.HK 0460.HK 0553.HK 2382.HK 
0031.HK 0747.HK 3188.HK 1071.HK 3382.HK 3823.HK 3898.HK 0451.HK 2727.HK 0968.HK 
0750.HK 1680.HK 6136.HK 1072.HK 6823.HK 1177.HK 2020.HK 0419.HK 6828.HK 1060.HK 
8047.HK 0867.HK 0336.HK 1848.HK 1856.HK 1313.HK 2607.HK 3886.HK 8292.HK 1618.HK 
0572.HK 2211.HK 3336.HK 2313.HK 0220.HK 1323.HK 1638.HK 1185.HK 1004.HK 1808.HK 
8321.HK 0205.HK 2623.HK 2393.HK 0161.HK 1613.HK 0855.HK 8201.HK 0882.HK 1212.HK 
0696.HK 1375.HK 0091.HK 0038.HK 0911.HK 3360.HK 0085.HK 1333.HK 0152.HK 1522.HK 
0570.HK 0938.HK 1330.HK 2880.HK 3049.HK 0546.HK 2198.HK 1108.HK 8237.HK 2380.HK 
0996.HK 0402.HK 0036.HK 0732.HK 0444.HK 0895.HK 3393.HK 1345.HK 0476.HK 1369.HK 
1131.HK 1228.HK 0154.HK 0548.HK 8123.HK 0899.HK 0718.HK 2322.HK 0926.HK 1661.HK 
1089.HK 0811.HK 0433.HK 83188.HK 0303.HK 1728.HK 0260.HK 0107.HK 2348.HK 
1599.HK 1065.HK 8311.HK 8018.HK 0530.HK 8207.HK 0440.HK 1308.HK 0564.HK 0568.HK___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return Statement error

2014-10-13 Thread William Becerra
I am familiar with funtions, i didn't realize i had to  write the return
statement inside a function...Thank you all..that was very helpful
On 13 Oct 2014 01:03, "Steven D'Aprano"  wrote:

> On Sun, Oct 12, 2014 at 04:38:54PM +0200, William Becerra wrote:
> > Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS
> > I was making an application to see if I understand how the return
> statement
> > works
>
> The `return` statement can only be used inside a function. That means
> you have to start off with a `def` line, and indent your code.
>
> Have you learned about functions yet? If not, perhaps you might prefer
> to forget about `return` until you do. Otherwise, if you take the code
> you wrote, indent it, and put a function declaration at the top, you
> should be able to use `return` successfully:
>
> def compare():
> print "Please write a value for x"
> x = raw_input()
> print "Please write a value for y"
> y = raw_input()
> if x  > y:
> return 1
> elif x < y:
> return -1
> elif x == y:
> return 0
> else:
> return "this will never happen"
>
>
> Then, once you have defined your function, you can call it:
>
> result = compare()  # don't forget the parentheses ()
> print "And the result is", result
>
>
>
> --
> Steven
> ___
> 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