简体   繁体   English

如何制作猜数字游戏?

[英]how to make a guess number game?

im trying to make a guessing game, user can input the level she wants, if 1 just numbers between 1,9 and if 2 between 10,99 otherwise between 100,99... here is my code:我试图做一个猜谜游戏,用户可以输入她想要的级别,如果 1 只是 1,9 之间的数字,如果 2 介于 10,99 之间,否则在 100,99 之间......这是我的代码:

fals = 0
while fals < 10:
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10,99)
        y = random.randint(10,99)
    else: # Next convert to else
        x = random.randint(100,999)
        y = random.randint(100,999)
    answer = x + y

    while (guess := input(f'{x} + {y} = ')) != answer:
        fals += 1
        if fals == 3:
            print(f'{x} + {y} = {answer}')
            break

problem is when i input 1 or 2 or 3 it always returns a 3digit question like 395 + 341 --- anyway the other problem is i want to ask 10 questions and also for each question if user inputs the answer 3 time false, program will give the user correct answer and keep asking other question问题是当我输入 1 或 2 或 3 时,它总是返回一个 3 位数的问题,如 395 + 341 --- 无论如何,另一个问题是我想问 10 个问题,并且对于每个问题,如果用户输入了 3 次错误的答案,程序将给用户正确的答案并继续问其他问题

import random
fals = 1
while fals <= 10:
    level = int(input(str(fals)+". Enter level :"))
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10,99)
        y = random.randint(10,99)
    else: # Next convert to else
        x = random.randint(100,999)
        y = random.randint(100,999)
    answer = x + y

    i = 0
    while i < 3:
        guess = input(f'{x} + {y} = ')
        if int(guess) == answer:
            print("Correct")
            break
        i += 1
        if i == 3:
            print("Answer ",answer)
   fals+=1

Hope this code works.希望这段代码有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM