pycharm简单游戏代码
时间: 2023-07-05 18:28:14 浏览: 167
pygame示例代码
好的,我可以为你提供一个简单的猜数字游戏的代码。请按照以下步骤操作:
1. 打开PyCharm,创建一个新的Python项目。
2. 在项目中创建一个新的Python脚本文件。
3. 复制以下代码并将其粘贴到Python脚本文件中。
```
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number + '.')
```
4. 点击运行按钮(位于PyCharm工具栏中的绿色三角形),并在控制台中玩游戏。
希望这个简单的代码能够帮助到你!
阅读全文