I am trying to create a Rock, Paper, Scissors application using Python and flet. I get:
TypeError: main.<locals>.rock() takes 0 positional arguments but 1 was given
I don’t understand what the error is telling me. I am trying to get it to change “choices_text” from “Player vs Computer” to whatever each entity chose (“Player” to player’s choice, and “Computer” to whatever the randomizer selected for the computer.) Then “winner_textField” would output who won or if it was a tie.
Here is my code so far:
import flet as ft
import random
def main(page: ft.Page):
############ function ##################
def rock():
computer = random.choice(['rock', 'paper', 'scissors'])
if computer == 'rock':
choices_text.value = "Rock vs Rock"
winner_textField.value = "It's a tie!"
elif computer == "paper":
choices_text.value = "Rock vs Paper"
winner_textField.value = "Computer wins!"
else:
choices_text.value = "Rock vs Scissors"
winner_textField.value = "You win!"
page.update()
def paper():
computer = random.choice(['rock', 'paper', 'scissors'])
if computer == 'rock':
choices_text.value = "Paper vs Rock"
winner_textField.value = "You win!"
elif computer == "paper":
choices_text.value = "Paper vs Paper"
winner_textField.value = "It's a tie!"
else:
choices_text.value = "Paper vs Scissors"
winner_textField.value = "Computer wins!"
page.update()
def scissors():
computer = random.choice(['rock', 'paper', 'scissors'])
if computer == 'rock':
choices_text.value = "Scissors vs Rock"
winner_textField.value = "Computer wins!"
elif computer == "paper":
choices_text.value = "Scissors vs Paper"
winner_textField.value = "You win!"
else:
choices_text.value = "Scissors vs Scissors"
winner_textField.value = "It's a tie!"
page.update()
def reset():
choices_text.value = "Player vs Computer"
winner_textField.value = " "
page.update()
############# page setup ################
page.title="Rock Paper Scissors"
page.window_width = 500
page.window_height = 700
page.theme_mode = "light"
page.window_center()
page.vertical_alignment=ft.MainAxisAlignment.SPACE_EVENLY
page.horizontal_alignment=ft.CrossAxisAlignment.CENTER
########### page content ###############
title_text = ft.Text("Rock Paper Scissors", color="BLUE", size=30)
choices_text = ft.Text("Player vs Computer", color="BLACK", size=25)
########### output ###############
winner_textField = ft.TextField(width=300, height=100, color="BLACK", text_size=20, text_align="center")
########### input buttons ###############
rock_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("ROCK", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=rock)
paper_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("PAPER", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=paper)
scissors_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("SCISSORS", size=20, text_align="center")]), width=200, height=100, bgcolor="BLUE", color="WHITE", on_click=scissors)
############ reset button ###############
reset_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("Reset", size=20, text_align="center")]), width=300, height=100, bgcolor="BLUE", color="WHITE", on_click=reset)
page.add(
title_text,
choices_text,
rock_button,
paper_button,
scissors_button,
winner_textField,
reset_button
)
ft.app(target=main)