有次数限制的猜数字

import random
import tkinter
import sys
import tkinter.messagebox


def cai_shu_zi():
    global i
    if Button1['text'] == "游戏结束":
        sys.exit()
    else:
        try:
            text_check = ''.join(j for j in Entry1.get() if j in '0123456789')
            int_cin = int(text_check)
            if i == 5 and int_cin != n:
                Label2['text'] = "你已经猜了5次了"
                Button1['text'] = "游戏结束"
                Entry1.delete(0, 'end')
            elif int_cin < n:
                Label2['text'] = "你输入的数比较小"
                i += 1
                Button1['text'] = "输入下一个数字"
                Entry1.delete(0, 'end')
            elif int_cin > n:
                Label2['text'] = "你输入的数比较大"
                i += 1
                Button1['text'] = "输入下一个数字"
                Entry1.delete(0, 'end')
            elif int_cin == n:
                Label2['text'] = "恭喜你猜对了"
                Button1['text'] = "游戏结束"
                Entry1.delete(0, 'end')
        except ValueError:
            tkinter.messagebox.showerror(title='Error', message='请输入数字')


if __name__ == "__main__":
    n = int(random.random() * 50 + 1)
    i = 0
    window = tkinter.Tk()
    window.title('猜数字游戏')
    window.geometry('500x200')
    Label1 = tkinter.Label(window, text='猜数字游戏', font=('Arial', 12), width=30, height=2)
    Label1.place(x=110, y=0, anchor='nw')
    Button1 = tkinter.Button(window, text='开始游戏', font=('Arial', 12), width=20, height=1, command=cai_shu_zi)
    Button1.place(x=150, y=50, anchor='nw')
    Label2 = tkinter.Label(window, text='游戏未开始', bg='pink', font=('Arial', 12), width=50, height=2)
    Label2.pack(side='bottom')
    Entry1 = tkinter.Entry(window, width=7, show=None)
    Entry1.place(x=220, y=90, anchor='nw')
    window.mainloop()


Vite + Vue 3 + TypeScript 的项目中,你可以使用 Vue Router 来处理路由调用接口。 首先,确保你已经安装了 Vue Router 和 axios(或其他用于发送 HTTP 请求的库)。你可以使用以下命令进行安装: ``` npm install vue-router axios ``` 接下来,在你的项目中创建一个 `router` 文件夹,在其中创建一个 `index.ts` 文件。在该文件中,你可以配置你的路由。以下是一个示例: ```typescript import { createRouter, createWebHistory, RouteRecordRaw } from &#39;vue-router&#39;; import Home from &#39;@/views/Home.vue&#39;; import About from &#39;@/views/About.vue&#39;; const routes: Array<RouteRecordRaw> = [ { path: &#39;/&#39;, name: &#39;Home&#39;, component: Home, }, { path: &#39;/about&#39;, name: &#39;About&#39;, component: About, }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 在上面的示例中,我们定义了两个路由:`Home` 和 `About`。你可以根据你的需求进行修改和扩展。 然后,在你的入口文件(如 `main.ts`)中,引入使用你的路由: ```typescript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import router from &#39;./router&#39;; const app = createApp(App); app.use(router); app.mount(&#39;#app&#39;); ``` 现在,你可以在你的组件中使用 `vue-router` 进行路由导航和调用接口。例如,在 `Home.vue` 组件中,你可以这样使用: ```vue <template> <div> <h1>Home</h1> <button @click="fetchData">Fetch Data</button> </div> </template> <script> import axios from &#39;axios&#39;; export default { methods: { fetchData() { axios.get(&#39;/api/data&#39;).then((response) => { console.log(response.data); }).catch((error) => { console.error(error); }); }, }, }; </script> ``` 在上面的示例中,我们使用了 axios 发送 GET 请求来获取数据。你可以根据你的需求调整和扩展这个例子。 希望这能帮助到你!如果你有任何其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值