0

I have been experiementing with this code for a couple hours and have come across an issue with each implementation. I can no longer put in any time at the moment so I figured I would go ahead and ask. As of now it keeps telling me that "else" is invalid syntax and when I try to implement it without the else my returns have issues and are outside the function. Here is my code:

from cs50 import get_int

condition = True
# originally n < 1 or n >8
while condition:
    n = get_int("Height:") in range(8)
    if n != range:
        print("Pick a number between 1 and 8 please!")
    return 
    else:
        return False
for i in range(n):
    for j in range(n):
        if i + j < n - 1:
            print(" ")
        else:
            print("#")

Updated code:

from cs50 import get_int

condition = True
# originally n < 1 or n >8
while condition:
    n = get_int("Height:") in range(8)
    if n > 0 and n < 8:
        break
for i in range(n):
    for j in range(n):
        if i + j < n - 1:
            print(" ")
        print("#")
1
  • I also implemented a version without returns but it would only print one # and it would still print the error message before printing the #. Commented Jul 20, 2020 at 1:53

3 Answers 3

1

Just like in C there were couple of bugs you have noted when you try to pair if and else and certain syntax error

I think it makes the computer to extra think as it should not because obviously computer is smart enough to know and understand when if statement is false don't execute it rather move to next line which is actually an else statement but you don't need to tell the computer that

hey, computer this is else statement when you are not happy with if statement

So you shouldn't do that, remove else from the code and try some different approach

Note: I am not gonna tell you the key because it is a practice based PSET for python which makes your python basics strong.

2
  • I tried to remove the else and I could not get the function to move on to the for loop, I changed my if statement and it moved on to the loop but the loop only prints out on # when I input 2. I changed the range in the loops from n to an actual number and it just prints out one long row of # when I input 2. Is there no way I can use range in my if statement? Because if n != range: did not work. Also, can you point me in a direction to figure out why my for loops are not working? Thank you. Updated code found with original. Commented Jul 24, 2020 at 23:34
  • I also practiced with: n = 8 for x in range(n): for y in range(n): print(x, y) and that works so I am not sure why it won't work on my mario file. I also forgot to mention I tried setting the range to n - 1 in addition to using numbers and it still does not work. Commented Jul 25, 2020 at 3:19
1

I think its because after your if argument, your return's indentation matches if's indentation. if and else should be consecutive and should not have anything other than argument between them. Try changing indentation of your return after if.

1
  • I no longer get error msg but when I put in a height in my range it still prompts me as if it is not in the range. I changed the if line and added (8) after range and it still did not work. ``` from cs50 import get_int condition = True # originally n < 1 or n >8 while condition: n = get_int("Height:") in range(8) if n != range: print("Pick a number between 1 and 8 please!") else: condition = False break for i in range(n): for j in range(n): if i + j < n - 1: print(" ") else: print("#") ``` Commented Jul 24, 2020 at 23:13
0

i have a feeling that range(8) is messing things up. when you say range(8) it means from 0 to 7.. Try removing range while getting the input from user and only check it once like you are doing with if n < 0 or n > 8 statement.

5
  • I tried removing it and when I input 2 it gave me 4 # in a vertical row. I also tried changing the range to 9 and it functioned the same as it did when it was eight except it accepted 0 as input for n. For some reason I feel like the issue has to be with the for loops because I cannot think of what else it can be if it isn't the range(8) throwing everything off. Commented Jul 25, 2020 at 20:41
  • did you make any changes to for loop or is it same as the updated code you have posted here?
    – Himanshi
    Commented Jul 26, 2020 at 12:31
  • No, everything was exactly the same except for deleting range(8) Commented Jul 27, 2020 at 0:49
  • your for loop is messed up a bit. check your logic in mario.c.. logic remains the same. only syntax is different
    – Himanshi
    Commented Jul 27, 2020 at 10:52
  • I cannot figure out what the issue with it is, it seems the only difference from my mario.c is the syntax Commented Jul 31, 2020 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .