-2

when I try to run this module

from import random
def guessnum():
    randomnum = random.randint(1,6)
awnser = input ("what do you think the number is? ")
if awnser==randomnum:
   print ("good job. you are correct. ")
else:
     print ("incorrect. better luck next time. ")
restart = input ("would you like to try again? ")
if restart = Yes or y:
guessnum()
else:
     end()

I get invalid syntax highlighting the import.

what is the issue?

I have already tried import random but it doesn't seem to want to work

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Scratchndent
  • 3
  • 1
  • 3
  • 1
    Looks like you'd better get back to whatever resource you have for learning Python: There's a surprisingly large number of errors in that small amount of code. – Biffen Feb 14 '17 at 05:42
  • 1
    fix your import back to the correct `import random` and your indentation and if you still have a problem say explicitly what it is. – Julien Feb 14 '17 at 05:43

2 Answers2

0

Fixed your indentation, spelling, capitalization, insertion of unnecessary spaces and comparing between a string and int which would always be false.

Also added str.title to allow for all types of capitalization for restart

import random
import sys

def guessnum():
  random_num = random.randint(1,6)
  answer = int(input("What do you think the number is? "))
  if answer == random_num:
    print("Good job. you are correct!")
  else:
    print("Incorrect. Better luck next time. The number was %d" % random_num)
  restart = input("Would you like to try again? ")
  if restart.title() in ["Yes", "Y"]:
    guessnum()
  else:
    end()

def end():
  print("Goodbye!")
  sys.exit(0)

guessnum()
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • thankyou @shash678 but would this still work if i joined multiple scripts together or would i have to put import at every individual script if i wanted to create a "game room? – Scratchndent Feb 21 '17 at 01:24
  • @Scratchndent What your asking is covered in this question http://stackoverflow.com/a/1057765/6328256 but I have a feeling it might be a little to advanced for you... so while you are still new just put the same import statements at the top of each of your `game` scripts. By the way would you be able to mark my answer as correct/helpful by hitting the green tick button? – Sash Sinha Feb 21 '17 at 03:39
0

Your code is full of errors. I have fixed indentation and other syntax issues. You don't need to use from, just use import random.

Here is the code

import random

def guessnum():
  randomnum = random.randint(1,6)
  awnser = input ("what do you think the number is? ")
  if awnser==randomnum:
    print ("good job. you are correct. ")
  else:
    print ("incorrect. better luck next time. ")

restart = input ("would you like to try again? ")
if restart == "Yes" or "y":
  guessnum()
else:
  end()
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189