为什么只显示功能和文本显示?

发布于 2025-02-12 18:08:30 字数 248 浏览 0 评论 0原文

我一直在尝试将用户输入加在一起,从而将其逐一计算到零。有时候,我脑海中出现了,但我会取得意外的结果。我真的想知道为什么没有显示文本。感谢您的帮助。

import time

def timer():
    for x in range(60,-1,-1):
        print(x)
        time.sleep(1.5)


input(f"Type code in {timer()} seconds : ")

I have been trying to make a user input with seconds together which counting down one by one till Zero. Sometimes somethings comes into my head but i take unexpected results. I am really wondering why the texts aren't being shown. Thanks for help from now.

import time

def timer():
    for x in range(60,-1,-1):
        print(x)
        time.sleep(1.5)


input(f"Type code in {timer()} seconds : ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2025-02-19 18:08:30

什么错...

您会遇到错误代码> timer()并尝试从中获取返回的值,然后用f'type代码在{value}秒中打印它:'

这就是为什么您将在屏幕上获得60 ... 0的计数,然后在非秒内输入代码:,为timer()timer()< /代码>返回什么都没有(<代码>无)。


该怎么做...

渲染续订显示并尝试同时在命令提示符中获得用户输入是理想的,即使不是不可能。 (命令提示不适合幻想。)

为了实现您的目标,我建议使用UI(用户界面),这是一个简单的UI:

import tkinter as tk

class UI():
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("200x50")
        self.data = tk.StringVar()
        self.label = tk.Label(text="")
        self.entry = tk.Entry(textvariable=self.data)
        self.label.pack()
        self.entry.pack()
        
        self.x = 60
        self.count_down()
        
        self.entry.bind("<Return>", self.end)
        self.root.mainloop()
        
        
    def count_down(self):
        if self.x < 0:
            self.root.destroy()
        else:
            self.label.configure(text=f"Type code in {self.x} seconds : ")
            self.x -= 1
            self.root.after(1500, self.count_down)
        
    def end(self, keypress):
        self.root.destroy()
    
app = UI()
value = app.data.get()

# do whatever you want with value
print('value : ',value)

What is wrong...

You are getting error because when you call input(f"Type code in {timer()} seconds : "), the program will run timer() and try to get the returned value from it, then print it with f'Type code in {value} seconds : '.

That is why you will get the count down of 60...0 on your screen, followed by Type code in None seconds :, as timer() return nothing(None).


What to do...

Rendering a renewing display and trying to get user input at the same time in the command prompt is not ideal if not impossible. (Command prompt is not for fancy display.)

To achieve your goal, i suggest using an UI (user-interface), here is a simple UI to do it:

import tkinter as tk

class UI():
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("200x50")
        self.data = tk.StringVar()
        self.label = tk.Label(text="")
        self.entry = tk.Entry(textvariable=self.data)
        self.label.pack()
        self.entry.pack()
        
        self.x = 60
        self.count_down()
        
        self.entry.bind("<Return>", self.end)
        self.root.mainloop()
        
        
    def count_down(self):
        if self.x < 0:
            self.root.destroy()
        else:
            self.label.configure(text=f"Type code in {self.x} seconds : ")
            self.x -= 1
            self.root.after(1500, self.count_down)
        
    def end(self, keypress):
        self.root.destroy()
    
app = UI()
value = app.data.get()

# do whatever you want with value
print('value : ',value)
谎言月老 2025-02-19 18:08:30

正如Tdelaney和Darryig在评论中所说的那样,在执行代码代码段时,它将输出60​​,59,58 ...,直到0,然后在0中显示“键入代码”:“我认为这是正确的结果。
仅在{}中放置一个简单的表达式(例如变量),代码代码段可以将表达式输出和文字输出。

Time = 5
input(f"Type code in {Time} seconds : ")

上面的代码段将输出“在5秒内输入代码:”

As tdelaney and DarryIG said in the comment, when executing the code snippet, it will output 60,59,58..., until 0, and then display "Type code in None seconds : ", I think this is the right result.
only place a simple expression(such as a variable) in the {}, the code snippet can output the expression and literal together.

Time = 5
input(f"Type code in {Time} seconds : ")

The code snippet above will output"Type code in 5 seconds : "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文