我正在尝试为我的计算功课做一个猜数字游戏,但我不知道如何限制用户只输入某些整数(即:1-100,也包括1和100)这是我的代码到目前为止:
import random
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
guess = int(input("Take a guess: "))
if guess > 100:
print ('That is an error please enter a number between 1 and 100')
if guess < 1:
print ('That is an error please enter a number between 1 and 100')
if guess > the_number:
print("The number is lower")
elif guess < the_number:
print("The number is higher")
else:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
break
else:
print("You failed to guess in time!\n\n")
如果你能帮助我,我们将不胜感激
答案 0 :(得分:0)
您可以尝试使用此基本代码来选择1到100之间的数字
import random
num = random.randint(1, 100)
while True:
print('Guess a number between 1 and 100')
guess = input()
i = int(guess)
if i == num:
print('You won!!!')
break
elif i < num:
print('Try Higher')
elif i > num:
print('Try Lower')
#any recommendations for the game end
print('if you gussed less than 6 times you won')
答案 1 :(得分:0)
def prompt_number_range(start, end):
while True:
prompt_str = "Please enter a number within the range {0} - {1}:".format(start, end)
try:
user_input = raw_input(prompt_str)
except NameError:
user_input = input(prompt_str)
try:
input_as_int = int(user_input)
if start <= input_as_int <= end:
return input_as_int
else:
print("that is not within the given boundaries")
except ValueError:
pass
print (prompt_number_range(1,100))
您可以在此查看上述内容:
https://ideone.com/9mtoOt(Python 3)
https://ideone.com/W6hRGh(Python 2.7)
以下是上面的代码合并到您的代码中完全正常运行:
import random
def prompt_number_range(start, end):
while True:
prompt_str = "Please enter a number within the range {0} - {1}:".format(start, end)
try:
user_input = raw_input(prompt_str)
except NameError:
user_input = input(prompt_str)
try:
input_as_int = int(user_input)
if start <= input_as_int <= end:
return input_as_int
else:
print("that is not within the given boundaries")
except ValueError:
pass
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
user_guess = prompt_number_range(*BOUNDS)
if user_guess == the_number:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
break
elif the_number < user_guess:
print("The number is lower")
elif the_number > user_guess:
print("The number is higher")
else: #for loop finished normally without breaking
print("You failed to guess in time!\n\n")
答案 2 :(得分:0)
尝试使用:
import random
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
win = False
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
guess = int(input("Take a guess: "))
if guess > 100:
print ('That is an error please enter a number between 1 and 100')
continue
elif guess < 1:
print ('That is an error please enter a number between 1 and 100')
continue
if guess > the_number:
print("The number is lower")
elif guess < the_number:
print("The number is higher")
elif guess == the_number:
win = True
break
if win == True:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
elif win == False:
print("You failed to guess in time!\n\n")
答案 3 :(得分:0)
这是你要的吗?
function blogRSS(url, container) {
var content = $('.data-content'); //console.log(content);
$.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
dataType: 'json',
success: function(data) {
console.log(data.responseData.feed);
$.each(data.responseData.feed.entries, function(key, value) {
var thehtml = '<h4><a href="' + value.link + '" target="_blank">' + value.title + '</a></h4>';
$("body").append(itemLayout);
$('.panel-heading').last().html(thehtml);
$('.data-content').last().html(value.content);
$('.data-title').last().next().slideToggle(0);
//console.log($('.panel-heading').last().html(value.title));
//console.log($('.panel-heading').closest('.data-title').text());
if(localStorage.favorite != null){
arr = JSON.parse(localStorage.favorite);
for(var i = 0; i < arr.length; i++){
// console.log($('.panel-heading').last().text() == arr[i].title);
if($('.panel-heading').text() == arr[i].title){
console.log('match');
$('.bkmark_icon').removeClass('glyphicon-star-empty');
$('.bkmark_icon').addClass('glyphicon-star');
//console.log('match');
}
}
}
}); $('.data-title').find('h4').addClass('panel-title');
答案 4 :(得分:0)
如果此代码的唯一问题是输入验证,则可以替换整个块:
computeTransFun()
有了这个:
void computeTransFun(const char *pat, int M, int TF[][NO_OF_CHARS])
答案 5 :(得分:0)