def __init__(self, *args, **kwargs): super(CaptchaForm, self).__init__(*args, **kwargs) # The super.__init__ call does not actually set the data or initial # onto the fields/widgets, so we just need the captcha_id field and # widget to exist before self.get_captcha() is called self.fields['captcha_id'] = forms.IntegerField() self.fields['captcha_id'].widget = forms.HiddenInput() if not self.is_bound: # New form, new captcha c = Captcha(text=get_string()) c.save() # Ensure that the hidden field is properly populated self.initial['captcha_id'] = c.id else: c = self.get_captcha() self.fields['captcha'] = CaptchaField(label="Turing test", captcha=c)
def get_captcha(self): # Somewhat hacky, but best way I had to get the captcha id f = self.fields['captcha_id'] w = f.widget #value = w.value_from_datadict(self.data, None, self.add_prefix('captcha_id')) #make it work in 0.96, where value_from_datadict() have 2 arguments value = w.value_from_datadict(self.data, self.add_prefix('captcha_id')) captcha_id = f.clean(value) try: # Always clean expired captchas before getting one for validation Captcha.clean_expired() return Captcha.objects.get(pk=captcha_id) except Captcha.DoesNotExist: # The original captcha expired, make a new one for revalidation c = Captcha(id=captcha_id,text=get_string()) c.save() return c
def __init__(self, data): self.name = util.get_string(data, "name") self.pid = util.get_string(data, "pid") self.start_time = util.get_date(data, "start_time") self.time_created = util.get_date(data, "time_created") self.time_started = util.get_date(data, "time_started") self.time_completed = util.get_date(data, "time_completed") self.url = util.get_string(data, "url") self.url_status = util.get_string(data, "url_status") self.url_headers = util.get_string(data, "url_headers") self.url_content = util.get_string(data, "url_content") self.status = util.get_string(data, "status") util.ensure(self.name, "name") util.ensure(self.start_time, "start_time") util.ensure(self.url, "url") util.ensure(self.status, "status")
def main(): computer_prompt = "Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly" ans = get_integer(0, 99, "Enter a number") guess = 0 low = 0 high = 100 while True: guess = (high + low) / 2 print(f"Is {guess} your secret number?") user_decision = get_string(1, 1, computer_prompt, accept_values=['h', 'l', 'c']) if user_decision == 'c': break elif user_decision == 'h': high = guess elif user_decision == 'l': low = guess print(f"High: {high}\nLow: {low}\nGuess: {guess}") print(f"Final guess: {round(guess)}")
def main(): main_deck = get_card_deck() shuffled_deck = main_deck[:] shuffle(shuffled_deck) player_cards, comp_cards, stack, pile = assign_cards(shuffled_deck) current_card = pile[-1] current_suite = pile[-1].suite is_player_turn = True player_choice = '' attack_value = 0 print_game_board(len(player_cards), len(comp_cards), current_card) while not is_game_over(player_cards, comp_cards, current_card): # Process Input (events) while is_player_turn: if player_choice == 'c': view_cards(player_cards, 'Your cards') player_choice = util.get_string(1,1, 'Press b to go back or p to play a card', accept_values=['b', 'p']) elif player_choice == 'p': player_card = play_card(player_cards, current_card, current_suite, attack_value > 0) if player_card != None and player_card != 1: input(f"You have played the {player_card} >>> ") pile.append(player_card) current_card = pile[-1] current_suite = current_card.suite is_player_turn = True if current_card.value == 7 or current_card.value == 11 else False if current_card.value == 8: current_suite = change_suite() elif current_card.value in [0, 2]: attack_value += current_card.power_value elif player_card == 1: input(f"The player will take {attack_value} cards >>> ") take_cards(attack_value, player_cards, stack) check_stack(stack, pile) attack_value = 0 is_player_turn = False player_choice = '' elif player_choice == 't': take_cards(1, player_cards, stack) check_stack(stack, pile) input(f"You have taken the {player_cards[-1]} >>> ") is_player_turn = False player_choice = '' else: player_choice = util.get_string(1, 1, "Press 't' to take a card, 'c' to view your your deck or 'p' to play a card", ['c', 'p', 't']) if attack_value == 0 else 'p' else: if not is_game_over(player_cards, comp_cards, current_card): input("Its the computer's turn now! >>> ") comp_card = comp_play_card(comp_cards, current_card, current_suite, attack_value > 0) if comp_card != None and comp_card != 1: pile.append(comp_card) input(f"The computer has played the {comp_card} >>> ") current_card = pile[-1] current_suite = current_card.suite is_player_turn = False if current_card.value == 7 or current_card.value == 11 else True if current_card.value == 8: current_suite = comp_change_suite(comp_cards) elif current_card.value in [0, 2]: attack_value += current_card.power_value elif comp_card == 1: input(f"The computer will take {attack_value} cards >>> ") take_cards(attack_value, comp_cards, stack) check_stack(stack, pile) attack_value = 0 is_player_turn = True else: take_cards(1, comp_cards, stack) check_stack(stack, pile) # input(f"The computer has taken the {comp_cards[-1]} >>> ") input(f"The computer has taken a card >>> ") is_player_turn = True # Update print_game_board(len(player_cards), len(comp_cards), current_card) input("GAME OVER >>> ") if len(player_cards) == 0: input("Congratulations! You won!!! >>> ") elif len(comp_cards) == 0: input("You lost. Better luck next time. >>> ") input("Thank you for playing >>> ")