Python 每次尝试都给出一个提示(猜数字游戏)

Python 每次尝试都给出一个提示(猜数字游戏),python,Python,我正在制作一个需要用户输入数字的游戏。他们尝试了四次,如果他们的号码错了,他们会得到一个提示 我如何才能尝试在四次尝试中每次只给出一个提示用户必须获得正确的号码?例如,在用户第一次尝试出错后,我希望程序显示偶数或奇数提示 from random import randrange new_number = randrange(1, 101) class Hints: def __init__(self, new_number): self.new_number = new

我正在制作一个需要用户输入数字的游戏。他们尝试了四次,如果他们的号码错了,他们会得到一个提示

我如何才能尝试在四次尝试中每次只给出一个提示用户必须获得正确的号码?例如,在用户第一次尝试出错后,我希望程序显示
偶数或奇数
提示

from random import randrange
new_number = randrange(1, 101)

class Hints:
    def __init__(self, new_number):
        self.new_number = new_number
        
    def even_or_odd(self):
        if self.new_number % 2 == 0:
            print('Hint. The number is even.')
        else:
            print('Hint. The number is odd.')
            
    def multiple3to5(self):
        for x in range(3, 6):
            n = self.new_number % x
        if n == 0:
            print(f'Hint. The number is a multiple of {x}', end = ' ' )
            
    def multiple6to10(self):
        for x in range(6, 11):
            n = self.new_number % x
        if n == 0:
            print(f'Hint. The number is a multiple of x {x}', end = ' ')




print('Guess a number beteen 1 and 100.')
hint = Hints(new_number)

for x in range(1, 5):
    is_answer_given = False
    while not is_answer_given:
        try:
            user_input = int(input(f'Attempt {x}: '))
            is_answer_given = True
        except ValueError:
            print('Please enter a numerical value.')
            
    if user_input == new_number and x == 1:    #hint after first attempt
        print('You win!')
        break
    else:
        print('Incorrect!')
        hint.even_or_odd()
        
    if user_input == new_number and x == 2:    #hint after second attempt
        print('You win!')
        break
    else:
        print('Incorrect!')
        hint.multiple3to5()
        
    if user_input == new_number and x == 3:   #hint after third attempt
        print('You win!')
        break
    else:
        print('Incorrect!')
        hint.multiple6to10()
    
    if x == 4:
        print('You are out of attempts!')
        print(f'The number was {new_number}')
        break
        

你应该做一个if语句来检查正确答案。在else语句中,可以使用if-elif语句检查尝试次数

如果用户输入==新编号:
打印(“你赢了!”)
打破
其他:
打印('不正确!')
如果x==1:
提示:偶数或奇数()
elif x==2:
hint.multiple3to5()
elif x==3:
hint.multiple6to10()
其他:
打印('尝试次数已用完!')
打印(f'编号为{new_number}')
您在尝试时看到多个提示的原因是,对于多个if语句,它们的条件不是true,因此它们的“else”块被运行

来自随机导入范围
新编号=随机范围(1011)
课堂提示:
定义初始(自我,新编号):
self.new_number=新_number
自我选择={
1:self.偶数或奇数,
2:self.multiple3到5,
3:self.multiple6到10
}
def运行(自我,钥匙):
action=self.choices.get(键)
行动()
def偶数或奇数(自):
如果self.new\u编号%2==0:
打印('提示。数字为偶数')
其他:
打印('提示。数字为奇数')
def倍数3至5(自身):
对于范围(3,6)内的x:
n=自我更新编号%x
如果n==0:
print(f'Hint.数字是{x}',end=''的倍数)
其他:
打印(“好吧,不是3或5的倍数”)
def倍数6至10(自):
对于范围(6,11)内的x:
n=自我更新编号%x
如果n==0:
打印(f'Hint.数字是x{x}',end=''的倍数)
其他:
打印(“好吧,不是6或10的倍数”)
打印('猜一个介于1和100之间的数字')
提示=提示(新编号)
打印(新编号)
i=3
赢=错
当i>=1时:
答案是否给出=错误
虽然没有给出答案,但:
尝试:
user_input=int(输入(f'trunt{i}:'))
答案是真的吗
除值错误外:
打印('请输入一个数值')
如果用户\u输入==新的\u编号:
打印('您赢了!,号码是:{new_Number}')
赢=真
打破
提示.运行(i)
i-=1
如果没有获胜:
打印(“你输了!”)
打印(f“编号为{新编号}”)

因此,您试图通过使用
用户输入猜出您输入到
提示()
构造函数中的数字,即
新的\u数字。即使使用提示的逻辑,他们也只检查倍数/奇数/偶数。您将如何猜出数字?请解释您当前的代码如何不符合您的要求。从您的总体结构来看,这似乎是正确的。请提供预期的价格。显示中间结果与预期结果的偏差。我认为“多重”方法中的缩进也有问题。
if
子句不应该在循环中吗?另一方面,在检查可除性的函数中似乎有一个错误的缩进
如果n==0:
应该在for语句中。