Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

10-07-2014 , 11:28 PM
It's time we have this thread. Enough of us are attending schools or doing online work that it's probably better to have one mega-thread rather than bits and pieces posted in the LC thread here and there.

Two questions from me:

First, in c++, we had a simple assignment to take a sentence and make it all upper case. I did that just fine with a toupper() function. The odd thing is that when it went to turn the '.' at the end of the sentence to upper case, it wrote two periods instead of one.

I guess I could write a check to make sure the char getting toupper'd is actually an alpha, but I'm really curious why this happened.


Think I figured this one out. Adding a blank space to the end of the input file corrected the double period. Not entirely sure why that fixed it.

***********

Second, I may have posted this before so I apologize if I did. I'm using Visual Studio 2013, and whenever I go Debug > Start Without Debugging, I get two copies of a command prompt of the program I wrote.

The first one closes itself when it reaches the end. The second I can't do anything in until the first closes, but behaves normally when complete, giving the "Press any key to continue . . ." message and not disappearing until I do so.

This might be problematic because we are moving into file i/o and when we start appending files I don't want to have a file written to twice when it should only be done once.

Any thoughts? I have default settings for vs2k13 afaik

***********

Thank you to anyone who offers their insight and expertise in this thread. And to those who have done so over the years on this site. I know a number of us have been inspired by your advice and opinions to pursue exciting new career paths, and your guidance will always be appreciated.

Last edited by Anais; 10-07-2014 at 11:35 PM.
Programming homework and newbie help thread Quote
10-08-2014 , 12:08 AM
Quote:
Originally Posted by Anais
It's time we have this thread. Enough of us are attending schools or doing online work that it's probably better to have one mega-thread rather than bits and pieces posted in the LC thread here and there.

Two questions from me:

First, in c++, we had a simple assignment to take a sentence and make it all upper case. I did that just fine with a toupper() function. The odd thing is that when it went to turn the '.' at the end of the sentence to upper case, it wrote two periods instead of one.

I guess I could write a check to make sure the char getting toupper'd is actually an alpha, but I'm really curious why this happened.


Think I figured this one out. Adding a blank space to the end of the input file corrected the double period. Not entirely sure why that fixed it.
Hmmm, not sure either honestly doesn't make sense. The function toupper returns the input value if no conversion is performed.

***********

Quote:
Second, I may have posted this before so I apologize if I did. I'm using Visual Studio 2013, and whenever I go Debug > Start Without Debugging, I get two copies of a command prompt of the program I wrote.

The first one closes itself when it reaches the end. The second I can't do anything in until the first closes, but behaves normally when complete, giving the "Press any key to continue . . ." message and not disappearing until I do so.

This might be problematic because we are moving into file i/o and when we start appending files I don't want to have a file written to twice when it should only be done once.

Any thoughts? I have default settings for vs2k13 afaik

***********

Thank you to anyone who offers their insight and expertise in this thread. And to those who have done so over the years on this site. I know a number of us have been inspired by your advice and opinions to pursue exciting new career paths, and your guidance will always be appreciated.
I assume you are writing a console app for windows. Maybe post a sample program. If I get a chance I'll try this out tomorrow. Seems kind of odd but I am sure it is fixable and/or explainable.
Programming homework and newbie help thread Quote
10-08-2014 , 01:50 AM
Quote:
Originally Posted by Anais
Second, I may have posted this before so I apologize if I did. I'm using Visual Studio 2013, and whenever I go Debug > Start Without Debugging, I get two copies of a command prompt of the program I wrote.

The first one closes itself when it reaches the end. The second I can't do anything in until the first closes, but behaves normally when complete, giving the "Press any key to continue . . ." message and not disappearing until I do so.

This might be problematic because we are moving into file i/o and when we start appending files I don't want to have a file written to twice when it should only be done once.

Any thoughts? I have default settings for vs2k13 afaik
What do you mean, "I go Debug > Start Without Debugging" just hit the green triangle pointed to the right button that says, "Local Window Debugger" and it will run your program. If you want to debug add breaker points and it will go directly into debugging, you can disable all breaker points and then enable them at any time.

If you were to want to run two programs you could build the program then while it is running go back to visual studio, menu click "Debug" and "Detach all" to then be able to build your program again without having to close the first.

If you want, you can even run your program outside of visual studio.
Go to where your visual studio project's folder is and hold "shift" down and right click the folder of the .exe to view a command open a cmd with path here. Just type the program.exe into cmd and hit enter to run.
Programming homework and newbie help thread Quote
10-08-2014 , 04:22 AM
Its a common thing theyre told to run without debugging. When you do that the console wont automatically close at end of execution which might be helpful if they are outputting things in the command prompt.
Programming homework and newbie help thread Quote
10-08-2014 , 09:27 PM
Quote:
Originally Posted by myNameIsInga
Its a common thing theyre told to run without debugging. When you do that the console wont automatically close at end of execution which might be helpful if they are outputting things in the command prompt.
Makes no sense to me, just add a cin >> intY; at the very bottom before returning 0.
Programming homework and newbie help thread Quote
10-10-2014 , 01:35 AM
Quote:
Originally Posted by adios
I assume you are writing a console app for windows. Maybe post a sample program. If I get a chance I'll try this out tomorrow. Seems kind of odd but I am sure it is fixable and/or explainable.
Yes, it is console apps, and it only occurs on my windows 7 pc. The mac I use sometimes at work doesn't have the same problem with the same code. Neither do the windows pcs at school using 2012. It's just my version of 2013.

It's literally any program whatsoever.

Just ran a small bit of code and it looks like this:



The background cmd prompt is empty until the first one is run all the way thru. I cannot click the exit button to close the front cmd prompt, I have to do all the input.

Just noticed the two drive letters for the cmd prompts. I have vs2013 installed to my g: drive. Could that be the issue?
Programming homework and newbie help thread Quote
10-10-2014 , 06:58 PM
Update: reinstalled windows, have vs2013 on same drive as windows. Works as it should, finally
Programming homework and newbie help thread Quote
10-10-2014 , 08:04 PM
Quote:
Originally Posted by Anais
Update: reinstalled windows, have vs2013 on same drive as windows. Works as it should, finally
You know the funny thing is that I had this same issue when I look years back when I was in university. I had a gaming rig that had two ssds in it and I had the OS installed on the first with games and visual studio installed on the second.

I ended up using my school's laptop to do most of my code assignments just because I tended to all my stuff on laptop. I did notice that double window on desktop but never bothered to fix it because I did other stuff on other IDE on desktop.

Life played out where I just ended up getting an expensive macbook pro that can do everything I really need and getting rid of desktop.
Programming homework and newbie help thread Quote
10-11-2014 , 01:48 AM
a couple q's from an intro to CS/Python online course that i didn't understand the answers to:

Code:
iteration = 0
while iteration < 5:
    count = 0
    for letter in "hello, world":
        count += 1
        if iteration % 2 == 0:
            break
    print "Iteration " + str(iteration) + "; count is: " + str(count)
    iteration += 1
"1. How many times will the print statement be exectued?

2. What is the smallest value of the variable count that will be printed out (at the print statement)?"


the answer to the first question is 5, but i don't see why. i thought it would be 3, because i thought the code takes iterations 0, 2, 4 (%2==0 and < 5) and break to the print statement only on those.

for the second question the answer is 1, and i think i understand how the program is interpreted, but i guess i don't understand why the interpretation is logical. i understand that "letter" is bound to "h" and the count is incremented by 1, and then apparently control is passed to the "if %2==0" conditional. but i don't understand why the count doesn't finish iterating through the string and binding "letter" to 2, 3, ....12, before going on to the "if" conditional.

fwiw here's the output of the code:

Code:
Iteration 0; count is: 1
Iteration 1; count is: 12
Iteration 2; count is: 1
Iteration 3; count is: 12
Iteration 4; count is: 1
Programming homework and newbie help thread Quote
10-11-2014 , 08:15 AM
Sounds like you are misunderstanding the break statement. As soon as you hit the break statement you are jumping to the first statement outside of the for loop (the print statement). And it happens monopoly style: you go directly there and don't stop to collect any money.

For point 1, remember that the break statement is just exiting the for loop. So you're still going to do the print statement for every time you go through the while loop (5 times).
Programming homework and newbie help thread Quote
10-11-2014 , 08:39 AM
hmm yeah i misunderstood the break statement, thanks

also misunderstanding the "count += 1" expression apparently...i thought it evaluated +1 for every character before moving on to the "if iteration..." statement. just found pythontutor.com tho and it seems very helpful !
Programming homework and newbie help thread Quote
10-11-2014 , 08:59 AM
The c++ problem sounds like an off by one error at the end of your loop, because the toupper function just returns the same char if it can't be capitalized.
Programming homework and newbie help thread Quote
10-11-2014 , 10:12 PM
Is this a good place to ask interweb programming related questions too?
Programming homework and newbie help thread Quote
10-12-2014 , 01:46 AM
stuck on this problem, from same edx.org course:

"In this problem, you'll create a program that guesses a secret number!

The program works as follows: you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search, the computer will guess the user's secret number!

Here is a transcript of an example session":


Quote:
Please think of a number between 0 and 100!
Is your secret number 50?
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. l
Is your secret number 75?
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. l
Is your secret number 87?
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. h
Is your secret number 81?
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. l
Is your secret number 84?
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. h
Is your secret number 82?
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. l
Is your secret number 83?
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. c
Game over. Your secret number was: 83
I've tried a handful of similar variations of the following code, they all contain some kind of error.

Code:
high = 100
low = 0
guess = (high+low)/2
print "Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?"
x = (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 corectly.'))
while x != c:
    if x == h:
        high = guess
        guess = (high+low)/2
    elif x == l:
        low = guess
        guess = (high+low)/2
    else:
        print "I do not understand your input."
    break
if x == c:
    print "Game over. Your secret number was: " + str(x)
When I run this, after inputting something for x, i get the following error:

Quote:
Traceback (most recent call last):
File "C:\Python27\asdf.py", line 7, in <module>
while x != c:
NameError: name 'c' is not defined
I tried assigning integers to (h, l, c) at the beginning, but then i get a syntax error at my first "if" statement. Basically I have no clue how to get the program to do math operations based on non-integer/float input. Anyone steer me in the right direction?
Programming homework and newbie help thread Quote
10-12-2014 , 02:07 AM
Try this on a brand-new shell:

Code:
>>> c
## you'll get the same error, right?
This is a data-type issue. Think about how you'd get the command line to do this:

Code:
>>> [enter something here]
## and you get no error, right?
And once you figure that out, you'll see why the error occurs. One of the positives of using python is that you have this interactive environment. Whatever you do in the shell will often be the same sort of things you'll get in a live program.

Or, if you are so inclined, try doing incremental development by commenting everything out and having only this:

Code:
x = (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 corectly.'))
Run it, then:

Code:
>>> print(x)
## see what you get
>>> type(x)
## see what you get
You're issues are covering a lot of different topics, but let's start here, shall we?

Last edited by daveT; 10-12-2014 at 02:20 AM.
Programming homework and newbie help thread Quote
10-12-2014 , 03:39 AM
Quote:
Originally Posted by daveT
Try this on a brand-new shell:

Code:
>>> c
## you'll get the same error, right?
This is a data-type issue. Think about how you'd get the command line to do this:

Code:
>>> [enter something here]
## and you get no error, right?
And once you figure that out, you'll see why the error occurs. One of the positives of using python is that you have this interactive environment. Whatever you do in the shell will often be the same sort of things you'll get in a live program.

Or, if you are so inclined, try doing incremental development by commenting everything out and having only this:

Code:
x = (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 corectly.'))
Run it, then:

Code:
>>> print(x)
## see what you get
>>> type(x)
## see what you get
You're issues are covering a lot of different topics, but let's start here, shall we?
hmmmm I see, I need quotations otherwise the h/c/l are just empty variables (like the error told me lol).

Also just noticed at the end it should be str(guess) instead of str(x), meh.

Checked the code adding quotations and got rid of the error messages, but the program but the program didn't continue asking for guesses, realized my 'while' loop is f'd.

Trying this:

Code:
high = 100
low = 0
guess = (high+low)/2
print "Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?"
x = (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 corectly.'))
while x != "c":
    if x == "h":
        high = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        (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 corectly.'))
    elif x == "l":
        low = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        (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 corectly.'))
    else:
        print "I do not understand your input."
    break
if x == "c":
    print "Game over. Your secret number was: " + str(guess)
Blaahh I still don't get it. I assume there must be a way to do it without typing those raw_input and print statements inside each conditional, but I don't know how.

Wait !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I think I have it

Code:
high = 100
low = 0
guess = (high+low)/2
print "Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?"
x = (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 corectly.'))
while x == "c" or "l" or "h":
    if x == "h":
        high = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        x = (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 corectly.'))
    elif x == "l":
        low = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        x = (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 corectly.'))
    elif x == "c":
        print "Game over. Your secret number was: " + str(guess)
        break
    else:
        print "I do not understand your input."
        break
dammnit i swore i had it but nope

Last edited by pewpewpew; 10-12-2014 at 03:54 AM. Reason: fack that's not right either
Programming homework and newbie help thread Quote
10-12-2014 , 03:55 AM
On the first guess, try entering 'c' and see what happens.

I'm disappointed that your program passed.
Programming homework and newbie help thread Quote
10-12-2014 , 04:05 AM
I might have edited while you replied but 'c' works, I get:

Quote:
Please think of a number between 0 and 100!
Is your number 50?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.c
Game over. Your secret number was: 50
I think it works except for when there's an undefined answer, it stops after the next input, ex. using the number 78:

Quote:
Please think of a number between 0 and 100!
Is your number 50?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.l
Is your number 75?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.l
Is your number 87?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 81?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 78?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.correct
I do not understand your input.
Is your number 78?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.c
hrmm
Programming homework and newbie help thread Quote
10-12-2014 , 04:14 AM
yeah it didn't actually pass =[ I just ran it a few times with some different numbers and got it excited when it worked, lol me. then submitted and found the errors.
Programming homework and newbie help thread Quote
10-12-2014 , 04:17 AM
OK this one passed =O

facking break statements man lol =[

Is there a way to clean up all the print/raw_inputs?

Code:
high = 100
low = 0
guess = (high+low)/2
print "Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?"
x = (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 corectly.'))
while x == "c" or "l" or "h":
    if x == "h":
        high = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        x = (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 corectly.'))
    elif x == "l":
        low = guess
        guess = (high+low)/2
        print "Is your number " + str(guess) + "?"
        x = (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 corectly.'))
    elif x == "c":
        print "Game over. Your secret number was: " + str(guess)
        break
    else:
        print "I do not understand your input."
        print "Is your number " + str(guess) + "?"
        x = (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 corectly.'))
Programming homework and newbie help thread Quote
10-12-2014 , 04:18 AM
I'm getting floats, which I'm pretty sure isn't the spirit of this program.

Last edited by daveT; 10-12-2014 at 04:23 AM. Reason: posting way too fast, IMO.
Programming homework and newbie help thread Quote
10-12-2014 , 04:25 AM
Weird, mine doesn't produce floats

Quote:
Please think of a number between 0 and 100!
Is your number 50?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 25?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 12?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 6?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 3?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 1?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.h
Is your number 0?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.l
Is your number 0?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed corectly.l
Is your number 0?
Are you using version 3 maybe?

Last edited by pewpewpew; 10-12-2014 at 04:30 AM.
Programming homework and newbie help thread Quote
10-12-2014 , 04:40 AM
Here's a cleaner version of the code:

Code:
high = 100
low = 0
guess = 0
print("Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?")
x = ''
while type(x) is str:
    guess = (high + low) // 2
    x = input('Enter "h" to indicate the guess is too high. Enter "l" to indicate\
 the guess is too low.h Enter "c" to indicate I guessed corectly.')
    if x == "h":
        high = guess
    elif x == "l":
        low = guess
    elif x == "c":
        print("Game over. Your secret number was: " + str(guess))
        break
    else:
        print("I do not understand your input.")
    print("Is your number " + str(guess) + "?")

Last edited by daveT; 10-12-2014 at 04:43 AM. Reason: 2to3, duh...
Programming homework and newbie help thread Quote
10-12-2014 , 05:05 AM
I'm actually getting an error with your code, and I have no idea why:

Code:
Please think of a number between 0 and 100!
Is your number 0?
Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low.h Enter "c" to indicate I guessed corectly.h

Traceback (most recent call last):
  File "C:/Python27/davet.py", line 9, in <module>
    the guess is too low.h Enter "c" to indicate I guessed corectly.')
  File "<string>", line 1, in <module>
NameError: name 'h' is not defined
It says h/l/c/other are not defined, which doesn't make sense to me since putting them in quotations solved that problem for me...wtf :s

just double-checking to make sure i pasted it correctly:

Code:
high = 100
low = 0
guess = 0
print("Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?")
x = ''
while type(x) is str:
    guess = (high + low) // 2
    x = input('Enter "h" to indicate the guess is too high. Enter "l" to indicate\
 the guess is too low.h Enter "c" to indicate I guessed corectly.')
    if x == "h":
        high = guess
    elif x == "l":
        low = guess
    elif x == "c":
        print("Game over. Your secret number was: " + str(guess))
        break
    else:
        print("I do not understand your input.")
    print("Is your number " + str(guess) + "?")
Programming homework and newbie help thread Quote
10-12-2014 , 05:10 AM
Yeah, there is actually a bug in there. I've been drinking and should go to bed now.

Code:
high = 100
low = 0
guess = 0
print("Please think of a number between 0 and 100!\nIs your number " + str(guess) + "?")
x = ''
while type(x) is str:
    guess = (high + low) // 2
    print("Is your number " + str(guess) + "?")
    x = 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 corectly.')
    if x == "h":
        high = guess
    elif x == "l":
        low = guess
    elif x == "c":
        print("Game over. Your secret number was: " + str(guess))
        break
    else:
        print("I do not understand your input.")
This is the working code using python 3. Not sure why you are getting the error.

Last edited by daveT; 10-12-2014 at 05:16 AM.
Programming homework and newbie help thread Quote

      
m