Python Forum
Getting errors when trying to run programs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting errors when trying to run programs
#1
I tried to copy the code of a game to see what happens, but I'm getting the following error:
https://imgur.com/a/Usw3uCc
I am new to Python and I am learning from the book named Python 3 from Tutorials Point. I tried to compile one of the sample programs in the book, but I'm getting another error:
https://imgur.com/a/6OPMRci
Reply
#2
Without seeing exactly what the code is, it looks like maybe the game is trying to gram an image from online, and either that page is gone or the image is gone or doing it from your IP won't work.

It would basically seem like it is missing an image file your game needs to run. You will have to look through the code to find the images and set them to ones you have.
Reply
#3
Tried another program from the book: https://imgur.com/a/Ky1VkWH
I'm getting a syntax error
Reply
#4
(Apr-09-2020, 08:27 PM)whois1230 Wrote: Tried another program from the book: https://imgur.com/a/Ky1VkWH
I'm getting a syntax error

You'd need to show the error. It will tell you the line with the syntax error. Look at it and the line above it for typos or other errors.
Reply
#5
In python3 ,you should use parenthesis for print statements.
eg: print("Hello world")
In your code these parenthesis are missing.
Reply
#6
(Apr-09-2020, 06:48 PM)michael1789 Wrote: Without seeing exactly what the code is, it looks like maybe the game is trying to gram an image from online, and either that page is gone or the image is gone or doing it from your IP won't work.

It would basically seem like it is missing an image file your game needs to run. You will have to look through the code to find the images and set them to ones you have.
I managed to get the game running. I believe this game was intended for Python 2. Rolling the dice

#!/usr/bin/python3
import random
min = 1
max = 6

roll_again = "yes"

while roll_again == "yes" or roll_again == "y":
    print ("Rolling the dices...")
    print ("The values are....")
    print (random.randint(min, max))
    print (random.randint(min, max))

    roll_again = input("Roll the dices again?")
What I did was to add parentheses to the print statements and delete the raw_ part of input in the last line.
Reply
#7
https://imgur.com/a/6OPMRci
I managed to fix the 'indented block' problem, but now I'm getting this('break' outside loop):
https://imgur.com/a/FB8RvGQ
Reply
#8
(Apr-10-2020, 11:21 AM)whois1230 Wrote: https://imgur.com/a/6OPMRci
I managed to fix the 'indented block' problem, but now I'm getting this('break' outside loop):
https://imgur.com/a/FB8RvGQ

The code still isn't indented properly. The 'break' needs to be inside the while loop, so does the if statement, I assume. The sys.exit() should probably be indented to be under the 'except' block.

If you could paste the code here, it would be much easier to fix.
Reply
#9
(Apr-10-2020, 11:34 AM)DreamingInsanity Wrote:
(Apr-10-2020, 11:21 AM)whois1230 Wrote: https://imgur.com/a/6OPMRci
I managed to fix the 'indented block' problem, but now I'm getting this('break' outside loop):
https://imgur.com/a/FB8RvGQ

The code still isn't indented properly. The 'break' needs to be inside the while loop, so does the if statement, I assume. The sys.exit() should probably be indented to be under the 'except' block.

If you could paste the code here, it would be much easier to fix.
#!/usr/bin/python3
import sys
try:
# open file stream
    file = open(file_name, "w")
except IOError:
    print ("There was an error writing to", file_name)
sys.exit()
print ("Enter '", file_finish,)
print ("' When finished")
while file_text != file_finish:
    file_text = input("Enter text: ")
if file_text == file_finish:
# close the file
    file.close
break
file.write(file_text)
file.write("\n")
file.close()
file_name = input("Enter filename: ")
if len(file_name) == 0:
    print ("Next time please enter something")
sys.exit()
try:
    file = open(file_name, "r")
except IOError:
    print ("There was an error reading file")
sys.exit()
file_text = file.read()
file.close()
print (file_text)


https://imgur.com/a/Ky1VkWH
I indented the whole code, but I'm getting 'invalid syntax' on the line
elif opt in ("-i", "--ifile"):
#!/usr/bin/python3
import sys, getopt
def main(argv):
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
            print ('test.py -i <inputfile> -o <outputfile>')
            sys.exit(2)
            for opt, arg in opts:
                if opt == '-h':
                    print ('test.py -i <inputfile> -o <outputfile>')
                    sys.exit()
            elif opt in ("-i", "--ifile"):
                        inputfile = arg
                        elif opt in ("-o", "--ofile"):
                            outputfile = arg
                            print ('Input file is "', inputfile)
                            print ('Output file is "', outputfile)
                            if __name__ == "__main__":
                                main(sys.argv[1:])
                                
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020