gpt4 book ai didi

python - 如何在 Pyscript 中获取显示消息

转载 作者:行者123 更新时间:2023-12-05 03:21:37 27 4
gpt4 key购买 nike

Pyscript 的新功能,请避免任何愚蠢的错误。

编写了一个简单的剪刀石头布游戏,如下所示,并通过网络浏览器运行。它运行良好,但我需要的是在提示中询问用户“输入 r 代表石头,p 代表纸,s 代表剪刀:”而不是像下面的屏幕截图中那样空白。

我试过 python.write,但它以某种方式抛出错误并且不确定如何使用它。非常感谢任何指导。

 <HTML>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-script>
import random

gameAttribute = ['rock','paper','scissor']

count=0
yourChoice=0
comChoice=0
print('==============================================================')
print('Welcome','Welcome to ROCK-PAPER-SCISSOR game, Come and try your luck!')
print('==============================================================')
while(count != 5):
count += 1
random_choice = (random.choices(gameAttribute))
computerMove = random_choice[0]
#print(random.choice(random_choice))
yourMove = input('Enter r for rock, p for paper and s for scissor:')


if yourMove == 'r':
yourMove = 'rock'
elif yourMove == 'p':
yourMove = 'paper'
elif yourMove == 's':
yourMove ='scissor'
else:
print("Choose between the given choice only!")

print('computer move: {} '.format(computerMove))
print("Your move:", yourMove)
if yourMove == computerMove:
print("It's a tie!\n")
elif yourMove == 'rock' and computerMove == 'paper':
comChoice += 1
print('You lose!\n')
elif yourMove == 'rock' and computerMove == 'scissor':
yourChoice += 1
print('You Win!\n')
elif yourMove == 'paper' and computerMove == 'rock':
yourChoice += 1
print('You Win!\n')
elif yourMove == 'paper' and computerMove == 'scissor':
comChoice += 1
print ('You lose!\n')
elif yourMove == 'scissor' and computerMove == 'paper':
yourChoice += 1
print('you Win!\n')
elif yourMove == 'scissor' and computerMove == 'rock':
comChoice += 1
print('You lose!\n')
else:
print('Invalid choice made!, choose between r,s,p only \n')

print('==============================================================')
if yourChoice > comChoice:
print('Congratulations! You have won the game')
elif comChoice > yourChoice:
print('You have lost the game.Better luck next time!')
elif yourChoice == comChoice:
print('Tough Competition! It is a tie')
print('==============================================================')
</py-script> </body>
</html>

enter image description here

最佳答案

使用 HTML 表单 请求用户输入可能更优雅。我刚刚导入了 bulma 以获得更好的元素。此外还有一个 bug已经报道了你的问题。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />

<!-- BULMA CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">

<!-- PYSCRIPT -->
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env></py-env>

</head>
<body>

<!-- UI -->
<div id="div1" class="container">
<div class="section">
<h4 class="title is-4">
Welcome to ROCK-PAPER-SCISSOR game, Come and try your luck!
</h4>

<br>
<input id="userInput"
type="text"
class="input is-info"
placeholder="have a guess"></input>
<br>

<label id="labelMessage" class="tag">Enter r for rock, p for paper and s for scissor:</label>

<br>

<button id="submit-guess-btn"
class="button is-primary"
type="submit"
pys-onClick="scissorGuess">Make A Guess</button>
</div>
<hr>
<div class="section">
<pre id=log></pre>
</div>
</div>

<py-script>
import random

gameAttribute = ['rock','paper','scissor']

count=0
yourChoice=0
comChoice=0
userElement = document.getElementById('userInput')
logElement = document.getElementById('log')

def scissorGuess(*args, **kwargs):
global count, yourChoice, comChoice, userElement, logElement
if count < 5:
count += 1
random_choice = (random.choices(gameAttribute))
computerMove = random_choice[0]
yourMove = userElement.value.strip()

if yourMove == 'r':
yourMove = 'rock'
elif yourMove == 'p':
yourMove = 'paper'
elif yourMove == 's':
yourMove ='scissor'
else:
logElement.innerHTML += "{}. Choose between the given choice only!\n".format(yourMove)

logElement.innerHTML += 'computer move: {}\n'.format(computerMove)
logElement.innerHTML += "Your move: {}\n".format(yourMove)
if yourMove == computerMove:
logElement.innerHTML += "It's a tie!\n"
elif yourMove == 'rock' and computerMove == 'paper':
comChoice += 1
logElement.innerHTML += 'You lose!\n'
elif yourMove == 'rock' and computerMove == 'scissor':
yourChoice += 1
logElement.innerHTML +='You Win!\n'
elif yourMove == 'paper' and computerMove == 'rock':
yourChoice += 1
logElement.innerHTML +='You Win!\n'
elif yourMove == 'paper' and computerMove == 'scissor':
comChoice += 1
logElement.innerHTML +='You lose!\n'
elif yourMove == 'scissor' and computerMove == 'paper':
yourChoice += 1
logElement.innerHTML +='you Win!\n'
elif yourMove == 'scissor' and computerMove == 'rock':
comChoice += 1
logElement.innerHTML +='You lose!\n'
else:
logElement.innerHTML += 'Invalid choice made!, choose between r,s,p only \n'

if yourChoice > comChoice:
logElement.innerHTML +='Congratulations! You have won the game\n'
elif comChoice > yourChoice:
logElement.innerHTML +='You have lost the game.Better luck next time!\n'
elif yourChoice == comChoice:
logElement.innerHTML +='Tough Competition! It is a tie\n'
</py-script>
</body>
</html>

输出:

Screenshot

关于python - 如何在 Pyscript 中获取显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72923420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com