Re: [Tutor] unorderable types
Curses! I screwed up my later insertion. I should have written in my footnote: On Sun, Aug 6, 2017 at 3:20 PM, boB Stepp wrote: > [1] An exception is "Beginning Python -- From Novice to Professional, > 3rd ed." by Magnus Lie Hetland, c. 2017. I recently acquired this > book and so far I am enjoying it. I would not recommend it for a true > beginner to programming, but for anyone that has had any experience > programming in any language I think it might be a good read for such a > person to learn Python. The author seems to bring in all of the > syntactic elements, either directly or in sidebars/sections that can > be skipped for later reading. Perhaps unfortunately, he does not > provide many examples, thus making it fast-paced, but if you have > any programming experience, this can be an advantage as such a person > probably does not need a lot of repetitive examples. The latter section > of the book has ten substantial projects he goes through that have > mostly practical applications for real world examples. I might do a > more detailed review later once I finish the book to see if my initial > impression holds up. But so far I wish it were the first Python book > I ever picked up! > > -- > boB -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
On Sun, Aug 6, 2017 at 1:23 PM, Mats Wichmann wrote: > Meanwhile, it is worth pointing out that while: (as with other python > loops) can take an else: clause, which is executed if the loop runs to > completion and was not exited via break. That means you could ALSO > write (this is pseudo-code for brevity): > > while guessesTaken < 6: > collect_guess > if guess_value == number > break > other_actions > else: > print(guesses_ran_out) > quit_program > > # if we got here it's because a guess was correct > print(congratulations) > > Using this is no more or less correct than not using it, just pointing > it out for your learning about Python loops. Apparently some people > think while/else, for/else and so on are useless, or vile, or whatever. > But they're certainly a part of the language. Interesting. I have seen else clauses for loops in the official Python docs and books which try to be syntax-complete in their presentation, but I cannot recall ever seeing this presented in beginner and introduction books of which I have several [1]. Why is this? It does not seem a particularly difficult or confusing topic and it can be useful. Just wondering ... [1] An exception is "Beginning Python -- From Novice to Professional, 3rd ed." by Magnus Lie Hetland, c. 2017. I recently acquired this book and so far I am enjoying it. I would not recommend it for a true beginner to programming, but for anyone that has had any experience programming in any language I think it might be a good read for such a person to learn Python. The author seems to bring in all of the syntactic elements, either directly or in sidebars/sections that can be skipped for later reading. Perhaps unfortunately, he does not provide many examples, thus making it fast-paced. The latter section of the book has ten substantial projects he goes through that have mostly practical applications for real world examples, but if you have any programming experience, this can be an advantage as such a person probably does not need a lot of repetitive examples. I might do a more detailed review later once I finish the book to see if my initial impression holds up. But so far I wish it were the first Python book I ever picked up! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
On 08/06/2017 11:35 AM, boB Stepp wrote: > So these final two "if" groupings should be _outside_ your while loop: > > while guessesTaken < 6: > > > if guess_value == number: > print('good job, ' + myName + '! you guessed my number in', > guessesTaken, 'guesses!') > > else: > print('nope. the number i was thinking of was', number) > > Notice the differences in indentation between what is _inside_ the > while loop and what now follows _outside_ the while loop. Also > instead of two consecutive "if" blocks, I used the "if - else" > structure. If the "if" condition is not true then the code will > automatically execute the "else" block. Per what the others have > said, I did not convert "guessesTaken" and "number" to strings, The > print function will handle that for us. Also, with the print function > if items to be printed are listed as separate arguments, that is, > separated by commas, the default behavior of print is to insert a > single space in the place of each comma. Yes, to highlight this last comment, the code as written is building one string to pass to print, in other words it is similar to doing: x = chunk1 + chunk2 + chunk3 print(x) of course there's no assignment to 'x' taking place, I just use that to illustrate that print is called with one argument. and Bob is suggesting you can also let print do the combining: print(chunk1, chunk2, chunk3) it which case it applies a little formatting work for you (inserting the space). There will be lots more options to string formatting for print. Meanwhile, it is worth pointing out that while: (as with other python loops) can take an else: clause, which is executed if the loop runs to completion and was not exited via break. That means you could ALSO write (this is pseudo-code for brevity): while guessesTaken < 6: collect_guess if guess_value == number break other_actions else: print(guesses_ran_out) quit_program # if we got here it's because a guess was correct print(congratulations) Using this is no more or less correct than not using it, just pointing it out for your learning about Python loops. Apparently some people think while/else, for/else and so on are useless, or vile, or whatever. But they're certainly a part of the language. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
On Sat, Aug 5, 2017 at 1:28 PM, Howard Lawrence <1019sh...@gmail.com> wrote: > # this is a guess number game. > import random > > guessesTaken = 0 > > print('hello! What is your name?') > myName = input() > > number = random.randint(1, 20) > print('Well, ' + myName + ', i am thinking of a number between 1 and 20') > > while guessesTaken < 6: > print('take a guess.') > guess = input() > guess_value = int(guess) > > guessesTaken = guessesTaken + 1 > > print("type(guess_value)=",type(guess_value)) > print("type(number)=",type(number)) > > if guess_value < number: > print('your guess is too low.') > > if guess_value > number: > print('your guess is too high.') > > if guess_value == number: > break If the values are equal the "break" statement will exit your while loop and never see the following "if" block. > if guess_value == number: >guessesTaken = str(guessesTaken) >print ('good job, ' + myName + '! you guessed my number in ' + > guessesTaken + ' guesses!') And I suspect you only want to execute the following "if" block if the user did not guess the number in the provided six guesses. > if guess_value != number: > number = str(number) > print ('nope. the number i was thinking of was ' + number) So these final two "if" groupings should be _outside_ your while loop: while guessesTaken < 6: if guess_value == number: print('good job, ' + myName + '! you guessed my number in', guessesTaken, 'guesses!') else: print('nope. the number i was thinking of was', number) Notice the differences in indentation between what is _inside_ the while loop and what now follows _outside_ the while loop. Also instead of two consecutive "if" blocks, I used the "if - else" structure. If the "if" condition is not true then the code will automatically execute the "else" block. Per what the others have said, I did not convert "guessesTaken" and "number" to strings, The print function will handle that for us. Also, with the print function if items to be printed are listed as separate arguments, that is, separated by commas, the default behavior of print is to insert a single space in the place of each comma. Inside your while loop you could use "if - elif - else" instead of your chain of three "if" statements. Have you read about these yet? If yes, then it would be more natural (Pythonic) to do things this way: while guessesTaken < 6: ... if guess_value < number: ... elif guess_value > number: ... else: break Hope all of this makes sense. If not come back with questions. Cheers! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
On 06Aug2017 07:19, Alan Gauld wrote: On 05/08/17 19:28, Howard Lawrence wrote: if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) There is the problem, you convert number to a str before printing it. so next iteration of the loop your if test fails. You don't need the conversion in this case because print does it automatically. I should point out that print doesn't magicly let you "+" a str and an int. You would need to write: print ('nope. the number i was thinking of was ', number) You can see that that doesn't use "+". The print function calls str() on each of its arguments, then writes the result to the output. str() on the string returns itself, and str(number) returns the number in text form. You can see then that you don't need str(number) yourself, and therefore do not need to store it in a variable. Also, while you can bind a value of any type to any variable, as you have here by binding an int to "number" and then later a str to "number", it is not a great idea. A particular variable should usually always hold the same type of value; this storing of different types of values in the same variable contributed to your problem. Cheers, Cameron Simpson (formerly c...@zip.com.au) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
On 05/08/17 19:28, Howard Lawrence wrote: > if guess_value != number: > number = str(number) > print ('nope. the number i was thinking of was ' + number) There is the problem, you convert number to a str before printing it. so next iteration of the loop your if test fails. You don't need the conversion in this case because print does it automatically. If you did need it for some other purpose then you should store the result in a temp variable rather than in number itself. This is a good example of why you should post the whole of your code not just the snippet that you think is causing the problem... If you had posted this in the first message we would probably have spotted it right away. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unorderable types
# this is a guess number game. import random guessesTaken = 0 print('hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', i am thinking of a number between 1 and 20') while guessesTaken < 6: print('take a guess.') guess = input() guess_value = int(guess) guessesTaken = guessesTaken + 1 print("type(guess_value)=",type(guess_value)) print("type(number)=",type(number)) if guess_value < number: print('your guess is too low.') if guess_value > number: print('your guess is too high.') if guess_value == number: break if guess_value == number: guessesTaken = str(guessesTaken) print ('good job, ' + myName + '! you guessed my number in ' + guessesTaken + ' guesses!') if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) # this is a guess number game. import random guessesTaken = 0 print('hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', i am thinking of a number between 1 and 20') while guessesTaken < 6: print('take a guess.') guess = input() guess_value = int(guess) guessesTaken = guessesTaken + 1 print("type(guess_value)=",type(guess_value)) print("type(number)=",type(number)) if guess_value < number: print('your guess is too low.') if guess_value > number: print('your guess is too high.') if guess_value == number: break if guess_value == number: guessesTaken = str(guessesTaken) print ('good job, ' + myName + '! you guessed my number in ' + guessesTaken + ' guesses!') if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) # this is a guess number game. import random guessesTaken = 0 print('hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', i am thinking of a number between 1 and 20') while guessesTaken < 6: print('take a guess.') guess = input() guess_value = int(guess) guessesTaken = guessesTaken + 1 print("type(guess_value)=",type(guess_value)) print("type(number)=",type(number)) if guess_value < number: print('your guess is too low.') if guess_value > number: print('your guess is too high.') if guess_value == number: break if guess_value == number: guessesTaken = str(guessesTaken) print ('good job, ' + myName + '! you guessed my number in ' + guessesTaken + ' guesses!') if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) # this is a guess number game. import random guessesTaken = 0 print('hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', i am thinking of a number between 1 and 20') while guessesTaken < 6: print('take a guess.') guess = input() guess_value = int(guess) guessesTaken = guessesTaken + 1 print("type(guess_value)=",type(guess_value)) print("type(number)=",type(number)) if guess_value < number: print('your guess is too low.') if guess_value > number: print('your guess is too high.') if guess_value == number: break if guess_value == number: guessesTaken = str(guessesTaken) print ('good job, ' + myName + '! you guessed my number in ' + guessesTaken + ' guesses!') if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) # this is a guess number game. import random guessesTaken = 0 print('hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', i am thinking of a number between 1 and 20') while guessesTaken < 6: print('take a guess.') guess = input() guess_value = int(guess) guessesTaken = guessesTaken + 1 print("type(guess_value)=",type(guess_value)) print("type(number)=",type(number)) if guess_value < number: print('your guess is too low.') if guess_value > number: print('your guess is too high.') if guess_value == number: break if guess_value == number: guessesTaken = str(guessesTaken) print ('good job, ' + myName + '! you guessed my number in ' + guessesTaken + ' guesses!') if guess_value != number: number = str(number) print ('nope. the number i was thinking of was ' + number) = dont understand the error TypeError unorderable types 'int()' <' str()' run the code from cmd prompt also error inserted a print function before the first "if" statement which return type (guess_value) = type(number) = ==
Re: [Tutor] unorderable types
Typing the : print("type (guess_value)=", type (guess_value)) print("type (number)=",type(number) type (guess_value)= type (number)= == the code runs again then prints type guess_value =< class int> type number= = Now Traceback kicks in if guess_value < number: TypeError : unorderable types: int() < str() Ran the code from Cmd prompt Got TypeError not supported between Instance of 'int' and 'str' = It's seems up till If guess_value < number: guess_value is a integer also number which the computer generated = Still scratching my head-:( ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unorderable types
boB Stepp wrote: > Did the text of this error message change between Python 3.5 and 3.6? Yes: $ python3.5 -c '1 < ""' Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int() < str() $ python3.6 -c '1 < ""' Traceback (most recent call last): File "", line 1, in TypeError: '<' not supported between instances of 'int' and 'str' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unorderable types
On 04Aug2017 22:00, boB Stepp wrote: When I attempted to recreate his error message with the original code snippets he sent, I got something a bit different: py3: guess = input() 2 py3: guess < number Traceback (most recent call last): File "", line 1, in TypeError: '<' not supported between instances of 'str' and 'int' I don't know about the text of the message, but his original snippets included a: guess = int(guess) between those 2 lines. Anyway, we have his current code, which i can't made produce the error. Cheers, Cameron Simpson (formerly c...@zip.com.au) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unorderable types
On Fri, Aug 4, 2017 at 8:44 PM, Cameron Simpson wrote: > [ Back onto the tutor list. - Cameron ] > > On 04Aug2017 09:12, Howard Lawrence <1019sh...@gmail.com> wrote: >> >> This is the code from tutorial > > > Thank you. > >> import random >> guessesTaken =0 >> >> print ('hello what is your name') >> myName =input () >> >> number = random.randint(1,20) >> print ('well, ' + myName + ', I am thinking of a number between 1 and 20') >> >> while guessesTaken < 6: >>print ('take a guess') >>guess=input () >>guess_value =int (guess) > > > Spooky :-) So you have "guess" containing the string from input(), and > "guess_value" has the int. > >>guessesTaken = guessesTaken +1 >> >>If guess_value < number: # this is it >>print('your guess is too low') > > > Strange, this should work. Btw, "If" should be lower case "if". > > [...snip...] >> >> # I changed all the "guess" to "guess_value" got the same result! >> >> This is it: traceback ( most recent call last): >> File "C:/User/Shaun/guessGame.py", line 19, in >> If guess_value < number: >> typeError: unorderable types:int() < str () When I attempted to recreate his error message with the original code snippets he sent, I got something a bit different: py3: guess = input() 2 py3: guess < number Traceback (most recent call last): File "", line 1, in TypeError: '<' not supported between instances of 'str' and 'int' I am running Python 3.6.1 on Windows 7 64-bit. Off-list Howard told me that he is running Python 3.5 on Windows 7, using PyCharm as his editor. Did the text of this error message change between Python 3.5 and 3.6? Could how he is using PyCharm -- a full-fledged Python IDE -- be causing this problem? I once tried out PyCharm a few years back, but cannot recall if one has to save first before re-running the code from within PyCharm in order for it to see the most recent version or not. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unorderable types
[ Back onto the tutor list. - Cameron ] On 04Aug2017 09:12, Howard Lawrence <1019sh...@gmail.com> wrote: This is the code from tutorial Thank you. import random guessesTaken =0 print ('hello what is your name') myName =input () number = random.randint(1,20) print ('well, ' + myName + ', I am thinking of a number between 1 and 20') while guessesTaken < 6: print ('take a guess') guess=input () guess_value =int (guess) Spooky :-) So you have "guess" containing the string from input(), and "guess_value" has the int. guessesTaken = guessesTaken +1 If guess_value < number: # this is it print('your guess is too low') Strange, this should work. Btw, "If" should be lower case "if". [...snip...] # I changed all the "guess" to "guess_value" got the same result! This is it: traceback ( most recent call last): File "C:/User/Shaun/guessGame.py", line 19, in If guess_value < number: typeError: unorderable types:int() < str () Interesting. Can you put this: print("type(guess_value) =", type(guess_value)) print("type(number) =", type(number)) above that line and run it again? Because I just ran your code here and it worked for me. Hope this can help you and mostly me I don't wanna give up but help is scarce from my location That's fine. The list is for help. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor