Getting errors when trying to run programs - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Getting errors when trying to run programs (/thread-25731.html) |
Getting errors when trying to run programs - whois1230 - Apr-09-2020 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 RE: Getting errors when trying to run programs - michael1789 - Apr-09-2020 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. RE: Getting errors when trying to run programs - whois1230 - Apr-09-2020 Tried another program from the book: https://imgur.com/a/Ky1VkWH I'm getting a syntax error RE: Getting errors when trying to run programs - michael1789 - Apr-10-2020 (Apr-09-2020, 08:27 PM)whois1230 Wrote: Tried another program from the book: https://imgur.com/a/Ky1VkWH 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. RE: Getting errors when trying to run programs - sravs - Apr-10-2020 In python3 ,you should use parenthesis for print statements. eg: print("Hello world") In your code these parenthesis are missing. RE: Getting errors when trying to run programs - whois1230 - Apr-10-2020 (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.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. RE: Getting errors when trying to run programs - whois1230 - Apr-10-2020 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 RE: Getting errors when trying to run programs - DreamingInsanity - Apr-10-2020 (Apr-10-2020, 11:21 AM)whois1230 Wrote: https://imgur.com/a/6OPMRci 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. RE: Getting errors when trying to run programs - whois1230 - Apr-10-2020 (Apr-10-2020, 11:34 AM)DreamingInsanity Wrote:(Apr-10-2020, 11:21 AM)whois1230 Wrote: https://imgur.com/a/6OPMRci #!/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:]) |