pycharm猜数字
时间: 2023-11-21 12:58:51 浏览: 73
以下是使用PyCharm实现猜数字游戏的代码,代码中使用了Python的random模块生成随机数,使用了PyQt5库实现了游戏界面。你可以将代码复制到PyCharm中运行,体验一下猜数字游戏。
```python
import random
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox
class GuessNumber(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Guess Number')
self.label1 = QLabel('Guess a number between 1 and 100:', self)
self.label1.move(20, 20)
self.textbox = QLineEdit(self)
self.textbox.move(20, 50)
self.textbox.resize(200, 25)
self.button = QPushButton('Guess', self)
self.button.move(20, 90)
self.button.clicked.connect(self.checkGuess)
self.label2 = QLabel('', self)
self.label2.move(20, 130)
self.show()
def checkGuess(self):
guess = int(self.textbox.text())
answer = random.randint(1, 100)
if guess == answer:
QMessageBox.information(self, 'Result', 'You guessed it right!')
elif guess < answer:
self.label2.setText('Your guess is too low.')
else:
self.label2.setText('Your guess is too high.')
if __name__ == '__main__':
app = QApplication([])
guessNumber = GuessNumber()
app.exec_()
```
阅读全文