无法获取要分配给变量的值

时间:2016-02-21 17:26:45

标签: python

import random
print('-\n')
begin=int(raw_input('-\n')
end=int(raw_input('-\n')
rep=int(raw_input('-\n')
def dop():
    print random.randinterga
ivfhoierh
while count < rep:
    print do
    count = count + 1

print('Thanks for using this program!\n')
raw_input('press enter to continue')

好的,所以我真的不知道我做错了什么但是我一直收到语法错误而且IDLE突出显示'结束'。

编辑:C

2 个答案:

答案 0 :(得分:1)

这就是我要这样做的方式:

import random
print 'I will print out random integers from a range you specify.'

begin = int(raw_input('Please enter the starting range: '))
end = int(raw_input('Please enter the end range: '))
rep = int(raw_input('Please enter the repeat value: '))

def get_random():
    return random.randint(begin, end)

for _ in range(rep):
    print get_random()

注重细节非常重要。

解决的问题:

  • 不匹配的括号
  • 不一致的作业
  • print
  • 的使用不一致
  • 个人偏好而不是返回,然后打印
  • 为了清晰起见,重命名了函数do()
  • raw_input()不需要\n个新行
  • randint()对浮点值生成ValueError: non-integer arg 1 for randrange()感到不安;相应地施放raw_input()
  • (次要)更正输出中的拼写
  • 使用for循环替换while循环,删除不需要的变量count和赋值

希望这有帮助!

答案 1 :(得分:-1)

我猜你使用的是Python 2,用raw_input来判断。 将您的代码更正为:

import random
print 'Hi, I will print out random intergers from a range you specify.\n'
#No need for parenthesis
begin=float(raw_input('Please enter the starting range. \n'))
end=float(raw_input('Please enter the end range. \n'))
rep=float(raw_input('Please enter how many times you cant to repeat the function. \n'))
# ERROR you forgot an extra parenthesis on the end of each of the last 3 lines.
def do():
    print random.randint(begin, end)

count = 0
while count < rep:
    do()  # <- Parenthesis, and no need to print.
    count = count + 1

print 'Thanks for using this program!\n' #No need for parenthesis
raw_input('press enter to continue')

还有:

count = 0
while count < rep:
    do()  # <- Parenthesis, and no need to print.
    count = count + 1

可以替换为:

for count in range(rep):
    do()