“from import random”的语法无效

时间:2017-02-14 05:39:16

标签: python syntax

当我尝试运行此模块时

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()

我的语法高亮显示导入。

问题是什么?

我已经尝试import random,但似乎不想工作

2 个答案:

答案 0 :(得分:0)

修复了缩进,拼写,大小写,插入不必要的空格以及stringint之间的比较,这些内容总是错误的。

还添加了str.title以允许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()

答案 1 :(得分:0)

您的代码充满了错误。我修复了缩进和其他语法问题。 您不需要使用,只需使用10, 25 and 16;

这是代码

import random