2017-11-11 1 views
0

Ich mache eine Funktion, die nach einer neuen Eingabe fragt, bis sie entweder Zahlen 0-8 oder 'X' erhält. Bisher habe ich das gemacht, aber es funktioniert nicht. Ich weiß, warum es nicht funktioniert, aber ich weiß nicht, wie es funktioniert.Make-Funktion akzeptieren 0-9 und X

def get_computer_choice_result(computer_square_choice): 
    print('What is hidden beneath square', computer_square_choice, '? (0 - 8 -> number of surrounding mines or X)') 
    field_content = input() 
    while not (ord(field_content) > ord('0') and ord(field_content) < ord('8')) or field_content != 'X': 
     field_content = input('Invalid input.Please enter either 0 - 8 -> number of surrounding mines, or X -> a mine.') 
    return field_content 

Antwort

0

Reguläre Ausdrücke sind perfekt für Ihre Bedürfnisse:

import re 

def get_computer_choice_result(computer_square_choice): 
    print('What is hidden beneath square', computer_square_choice, '? (0 - 8 -> number of surrounding mines or X)') 
    field_content = input() 
    while not re.match(r"([0-8]|X)$", field_content): 
     field_content = input('Invalid input.Please enter either 0 - 8 -> number of surrounding mines, or X -> a mine.') 
    return field_content 

Edit: Auch könnte Ihr Zustand arbeiten, aber es ist falsch. Es sollte folgendes sein:

while not (ord(field_content) >= ord('0') and ord(field_content) <= ord('8')) and field_content != 'X': 
Verwandte Themen