无效输入被拒绝,但随后不接受有效输入

时间:2018-11-18 16:49:13

标签: validation variables input

我已经用python创建了这个随机猜谜游戏,并且还将其扩展到了2人模式。但是,当它询问您有多少人在玩,而答案不是一两个时,它将转到“多人游戏!=” 1”或“ 2”的那一部分。然后,此代码向用户发出警告,指出该号码不是有效数字,并给了他们另一次机会。但是,即使给出有效的数字,它也不会做任何事情。

也请原谅我,如果我做错了什么,或者我真的错过了一些明显的事情,因为我还是Python的新手。

from random import *
import time
import os

class NotPositiveError(UserWarning):
    pass

continue_game = "Y"

#...(Unrelated stuff)...

while continue_game == "Y":
    time.sleep(1.7)
    #over here it changes whether you can play one or two player. If you say 
    #anything that isn't 1 or 2, then it skips down to the bottom where it 
    #says "while multiplayer != "1" or "2"
    multiplayer = input ("Would you like to play 1 or 2 player? ")
    while multiplayer == "1":
        correctNumber = randint(1, 100) 
        guessesTaken = 0
        change = input("I can give you a choice to change the amount of 
        guesses you get. This can make it harder or easier for yourself. 
        Would you like that? ")
        if change in ['Y', 'Yes', 'y', 'yes']:
            custom = input("Choose how many guesses you want: ")
            while True:
                try:
                    custom = int(custom)
                    if custom <= 0:
                            raise NotPositiveError
                    break
                except ValueError:
                        print("This was not a number, please try again. ")
                        custom = input("Choose how many guesses you want ")
                except NotPositiveError:
                        print("The number was not positive, please try 
                        again. ")
                        custom = input("Choose how many guesses you want: ") 

        #..(The guessing game)...

    while multiplayer == "2":
        max_num = 101
        min_num = 0

    #...(2 player guessing game)...

    #This is the place where I don't know what to do. When it says "please 
    #try again", it lets the user input something but doesn't do anything 
    #about it
    while multiplayer != "1" or "2":
        while True:
                    try:
                        multiplayer = int(multiplayer)
                        if multiplayer <= 0:
                                raise NotPositiveError
                        if multiplayer > 2:
                                print("There isn't a game mode for more than 
                                2 players. Please try again.")
                                multiplayer = input ("Would you like to play 
                                1 or 2 player? ")
                    except ValueError:
                            print("This was not a number, please try again. 
                            ")
                            multiplayer = input ("Would you like to play 1 
                            or 2 player? ")
                    except NotPositiveError:
                            print("The number was not positive, please try 
                            again. ")
                            multiplayer = input ("Would you like to play 1 
                            or 2 player? ")
                    break



    continue_game = input("Want another game? [Y/N](caps only) ")
    if continue_game in ['Y', 'Yes', 'y', 'yes']:
            multiplayer = input ("Would you like to play 1 or 2 player? ")

0 个答案:

没有答案
相关问题