Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Won't open from desktop
#1
I'm using python 3.6.5 to learn programming, and have come across a bit of a problem with my most recent lesson. The objective of the lesson is to create a small game where you input for the programme to randomly select a number between 1 and 100, with a loop created so that you can guess again until you get it right with the programme telling you whether to guess higher or lower, with a congratulations once you finally get it right. I initially had a problem where it wouldn't run through Run Module/f5, due to invalid syntax, but I sorted that out. Now, however, while it works exactly as it should when I "run module" (except press the enter key to exit, since it just puts me on a new line), it won't work when I try to open it from my desktop, it just flashes up for a fraction of a second, and then disappears. Any help you can give will be greatly appreciated. Here's the code I put in.

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money


print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")


# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop

while guess != the_number:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")

    guess = int(input("Take a guess: ")
    tries += 1

print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!\n")

input(\n\nPress the enter key to exit.")
Thank you for reading my thread, and for any answers you give.
Reply
#2
there is missing closing parenthesis on line#27
so this is not the code that runs in IDLE
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
True enough... Didn't think to just copy and paste it tbh. Here's a copied and pasted version of it

# Guess My Number
#
# The computer picks a number at random between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# Set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# Guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1

print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!\n")

input("\n\nPress the enter key to exit.")
Reply
#4
this one works for me. I guess you have more than one version of the file (e.g. the one from previous post and this one..)

also, make sure you don't have file named random.py in the same folder
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
There was an extra one which I deleted, and no random.py but it still won't work. All the other ones I have saved that I tried still work too :S

It turns out that I deleted the good save and was left with one with syntax errors in 3 places, thank you for your help :), deleting the other save did the trick nicely.
Reply


Forum Jump:

User Panel Messages

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