为什么我的代码为随机答案getter无效?

时间:2016-04-29 23:04:58

标签: python

基本上我为随机测试答案getter制作了一些代码。我的想法是它会给你一个选择的随机答案。当我运行代码时,似乎关闭了程序。

以下是代码:

import random
Question_total = int(input("How Many answers do you need?"))
x = random.choice('abcd')
print(x * Question_total) 

当我输入一个数字时,它就会退出。这是错误(我输入四个):

dddd
Exit status: 0
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

它完全应该做到的。仔细观察,您会看到它打印dddd。编写它的方式是,它从'abcd'获取一个值并将其打印Question_total次(x * Question_total)。如果您想要获取Question_total 不同的值,则需要多次调用random.choice('abcd')

Question_total = int(input("How Many answers do you need?"))

# Choose from 'abcd' Question_total different times and create a list of choices
choices = [random.choice('abcd') for _ in range(Question_total)]
print ' '.join(choices)

# a b d a

或者如果你想把它写成for循环

Question_total = int(input("How Many answers do you need?"))

choices = list()

# Go through the loop Question_total times
for x in range(Question_total):
    # Make a choice
    newchoice = random.choice('abcd')

    # Stick the choice in a list so we can remember it
    choices.append(newchoice)

    # Print the choice
    print newchoice

答案 1 :(得分:0)

代码正在做我应该做的事情。你给它一个数字,然后你选择一个随机字母并乘以该数字

在Python中,string * number返回连接到自身数字的字符串

你需要做的是做

var geocoder;
var map;

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(37.4419, -122.1419),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(
    document.getElementById("map"), mapOptions);
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  });
  google.maps.event.addDomListener(document.getElementById('dispmap'), 'click', function() {
    var elem = document.getElementById("map");
    console.log("before:" + elem.style.display);
    elem.style.display = (elem.style.display === 'none' ||
      elem.style.display === '') ? 'block' : 'none';
    console.log("after:" + elem.style.display);
    google.maps.event.trigger(map, 'resize');
    map.setCenter(mapOptions.center);
  })
}
google.maps.event.addDomListener(window, "load", initialize);
相关问题