Maker Pro
Maker Pro

Exchanging data variables?

This passes the test, however, I am having trouble with my error checking routing when incorrect entries are made. If I put the "
if choice != 'h' or 'l' or 'c':
I will need to look to be absolutely sure.
Typically programming languages break down the If statements:
So a simple "if choice != 'h'" would resolve to "if true" or "if false" depending on what choice != 'h' turns out to be.
So if you want to combine multiple conditions, you typically have to do it like this:
"if choice != 'h' or choice != 'l' or ...."
That way it would be broken down into
"if true or false or ...."

Of course, there is an alternative ;)
http://www.tutorialspoint.com/python/python_if_else.htm

If statements typically have additional keywords you can rely on.
Like "else"
Code:
If choice = 'h'
    print "too high!"
Else
    print "not too high"

Sometimes you can combine if and else in the same keyword.
Code:
If choice = 'h'
    print "too high!"
Elif choice = 'l'
    print "too low!"
Elif choice = 'c'
    print "just right!"
Else
    print "anything that didn't match above"

this is a little more detailed than I would have given sooner, but you already have an answer, so I'd help you with an additional tool.
 
I will need to look to be absolutely sure.
Typically programming languages break down the If statements:
So a simple "if choice != 'h'" would resolve to "if true" or "if false" depending on what choice != 'h' turns out to be.
So if you want to combine multiple conditions, you typically have to do it like this:
"if choice != 'h' or choice != 'l' or ...."
That way it would be broken down into
"if true or false or ...."

Of course, there is an alternative ;)
http://www.tutorialspoint.com/python/python_if_else.htm

If statements typically have additional keywords you can rely on.
Like "else"
Code:
If choice = 'h'
    print "too high!"
Else
    print "not too high"

Sometimes you can combine if and else in the same keyword.
Code:
If choice = 'h'
    print "too high!"
Elif choice = 'l'
    print "too low!"
Elif choice = 'c'
    print "just right!"
Else
    print "anything that didn't match above"

this is a little more detailed than I would have given sooner, but you already have an answer, so I'd help you with an additional tool.

I don't think this is an Else If situation as it needs to be if input is not either h,l or c - prompt error, rekey. But when I put this into my loop, no matter where I put it in the order of the loop I get strange results. Even though I am using or, it will typically print out at the end, two prompt error rekey statements since 'c' was depressed. I find this significant to the flow of logic going on - it tells me that python is looking at != 'h' or !='c' or != 'l' and testing each for !=. Since c is the last letter depressed indicating a correct answer, it checks against the argument and finds c == c and skips that, but fails it on the other two pieces of the argument - and prints twice. If I make separate statements it still happens. I tried if and elif, elif for each and it still gave errors!! This part is even more frustrating than coming up with the algorithm, which I can not take credit for ;-)

upload_2015-1-16_21-51-5.png
 
I don't think this is an Else If situation as it needs to be if input is not either h,l or c - prompt error, rekey. But when I put this into my loop, no matter where I put it in the order of the loop I get strange results. Even though I am using or, it will typically print out at the end, two prompt error rekey statements since 'c' was depressed. I find this significant to the flow of logic going on - it tells me that python is looking at != 'h' or !='c' or != 'l' and testing each for !=. Since c is the last letter depressed indicating a correct answer, it checks against the argument and finds c == c and skips that, but fails it on the other two pieces of the argument - and prints twice. If I make separate statements it still happens. I tried if and elif, elif for each and it still gave errors!! This part is even more frustrating than coming up with the algorithm, which I can not take credit for ;-)

View attachment 18164
That's where the else would work perfectly ;)
Because if it matches the above if or elif, it will ignore the else portion completely.
However... there is one change you can make to that last if statement.
Have you tried an 'and' instead of an 'or' ?
 
That's where the else would work perfectly ;)
Because if it matches the above if or elif, it will ignore the else portion completely.
However... there is one change you can make to that last if statement.
Have you tried an 'and' instead of an 'or' ?
I had thought of it but put it aside thinking it would compound my problems as it would require all to be not true inclusive. I will go and test it anyways, right now!

Edit: It works!!! so it forces the argument to search if any input is != l, c, h inclusively. I thought that or would do that...
Now to tidy the return of the %d. It should just insert the value of the variable, but instead its giving me %d. Is it a syntax error?

upload_2015-1-16_23-5-3.png
 
I had thought of it but put it aside thinking it would compound my problems as it would require all to be not true inclusive. I will go and test it anyways, right now!

Edit: It works!!! so it forces the argument to search if any input is != l, c, h inclusively. I thought that or would do that...
Now to tidy the return of the %d. It should just insert the value of the variable, but instead its giving me %d. Is it a syntax error?

View attachment 18165
I'm not sure what the syntax is for using %d is right now..
Personally, my attempt would be to do something like "was your number ", guess, "?"
This may not work, and may not be the cleanest approach though...
 
I'm not sure what the syntax is for using %d is right now..
Personally, my attempt would be to do something like "was your number ", guess, "?"
This may not work, and may not be the cleanest approach though...
I had that and then I had the variable in parenthesis. I think its a syntax issues between versions. Its quite annoying... but since its not allowing me to submit my answer for the online automatic grader, I can't finalize that section. Let me show you what their output is:

upload_2015-1-16_23-14-1.png

See the wording they use in the error - it was expecting a number. I wonder if the %d is throwing it off...
 
Can anyone spot the issue?
The issue is that the code needs to show the current guess immediately following a rejection of the last user input. Can't see a quick fix except for a revamping of the control structure. This is an example of why it is a bad idea to perform data calculations within an I/O routine. I'd be thinking about doing nested while loops.
 
Actually... lookin at the code, it may be simple enough to simply move the guess and print lines from inside the if statements to before all the if statements...
 
It was much simpler gents!! What was missing was a repeat of the line prompting if the secret number was correct when an error occurred!!!

Thanks everybody :D

Code:
mini = 0
maxi = 100
guess = (mini+maxi)/2
win = False

print 'Please think of a number between 0 and 100!'
print 'Is your secret number', guess, '?'
while (win == False):
choice = raw_input('Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed correctly. ')
if choice == 'h':
maxi = guess
guess = (mini+maxi)/2
print 'Is your secret number' , guess, '?'
if choice == 'l':
mini = guess
guess = (mini+maxi)/2
print 'Is your secret number' , guess, '?'
if choice != 'h' and choice != 'l' and choice != 'c':
print 'Sorry, I did not understand your input.'
print 'Is your secret number' , guess, '?'     #this is what was missing!! operates perfectly now
if choice == 'c':
print 'Game over. Your secret number was:' , guess
win = True
 
I suspect that the previous problem of printing with string formatting was due to using single quotes instead of double quotes.

print "Is your secret number %d?" % guess
 
Great! You have got it working now.

Now, how about a little refinement? You might notice that you have several lines that are exactly duplicated in multiple places. See if you can make these lines appear only once. This not only makes the program shorter and more elegant, but, if you decide you need to change something later, you can change only one occurrence and will not make the mistake of changing something 2 out of 3 places and missing the last one.

Bob
 
“Simplicity is the ultimate sophistication.”
I like to re-write and alter my code once I have the basics working to see how small I can make it :p

I want to rewrite chopnhack's code, but I'll wait to see what he does first :p
 
Makes sense, thanks guys!

I assume that you are talking about the print statement that asks if the guess is correct that appears three times. Would it be accomplished with a goto command pointing to a global print statement of "if guess is correct". Create function whose only purpose is to print the statement?
 
Makes sense, thanks guys!

I assume that you are talking about the print statement that asks if the guess is correct that appears three times. Would it be accomplished with a goto command pointing to a global print statement of "if guess is correct". Create function whose only purpose is to print the statement?
You could reposition it.
It would be a good practice to avoid GoTo .. it can get you in trouble.. at least for know.
 
Code:
mini = 0
maxi = 100
guess = (mini+maxi)/2
win = False

print 'Please think of a number between 0 and 100!'
print 'Is your secret number', guess, '?'
while (win == False):
         choice = raw_input('Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed correctly. ')
         if choice == 'h':
                maxi = guess
                guess = (mini+maxi)/2
       if choice == 'l':
                mini = guess
                guess = (mini+maxi)/2
       if choice != 'h' and choice != 'l' and choice != 'c':
               print 'Sorry, I did not understand your input.'
       print 'Is your secret number' , guess, '?'             #moving this over to run in the while clause saves space and allows the code to still work correctly.
       if choice == 'c':
                print 'Game over. Your secret number was:' , guess
                win = True
 
Top