这段代码:
import random
print("\tWelcome to the guess my number program\n")
print("im thinking of a number between 1 and 100")
print("try to guess it in 5 attempts\n")
randomNum = random.randint(1,101)
tries = 0
userNumber = int(input("please pick a number"))
while userNumber != randomNum:
if userNumber > randomNum:
print ("lower")
else:
print("higher")
tries += 1
print ("you guessed it! the number was" , randomNum)
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这会产生无限循环.任何帮助,我仍然习惯python.
我试图让对话框一次显示一个字符(就像在口袋妖怪游戏和其他类似游戏中一样)。
我在互联网上进行了搜索,但没有找到任何有用的东西。
我知道还有一个这样问的问题,但它没有解决我想要做的事情。我知道这是可以做到的,因为我见过用 python 制作的游戏已经做到了这一点。
我有几个点存储在一个数组中.我需要找到那些点的界限,即.限定所有点的矩形.我知道如何用普通的Python解决这个问题.
我想知道有没有比天真的最大,最小的数组或内置方法更好的方法来解决问题.
points = [[1, 3], [2, 4], [4, 1], [3, 3], [1, 6]]
b = bounds(points) # the function I am looking for
# now b = [[1, 1], [4, 6]]
Run Code Online (Sandbox Code Playgroud) 我想访问选定 Radiobutton 的值并将其与 if 语句进行比较,但在访问该值时,我得到的是 PY_VAR0。
from tkinter import *
ComplaintForm=Tk()
typesel=StringVar()#<--variable I'm using to access value of selected Radiobutton
HighVoltage=Radiobutton(ComplaintForm,text='High Voltage Motor',value='HighVoltage',\
anchor=W,font='roboto 18',bg='white',variable=typesel)
HighVoltage.grid(row=5,column=1,padx=5,pady=10)
LowVoltage=Radiobutton(ComplaintForm,text='Low Voltage Motor',value='LowVoltage',\
anchor=W,font='roboto 18',bg='white',variable=typesel)
LowVoltage.grid(row=5,column=0,padx=5,pady=10)
print(typesel)#this is printing PY_VAR0 instead of accessing value of above Radiobuttons
mainloop()
Run Code Online (Sandbox Code Playgroud)
PS:我知道这段代码中存在一些弊端,这些弊端是为了使代码最小化并使问题易于理解。
假设我想用__mul__方法定义一个类,例如:
class A():
def __init__(self,*arg,**kwarg):
#some code
def __mul__(self,other):
#some code
Run Code Online (Sandbox Code Playgroud)
如果我这样做A()*2会给我一个结果.但是,当我尝试相反的方式(2*A())时,它不起作用.有没有办法围绕操作实现这种其他方式?
我想创建一个确认删除意图的消息框.
def delete_action(self):
s_id = self.selected_ID_entry.get()
s_name = self.seletedEntry.get()
answer = messagebox.askquestion("Delete?","Are you sure you want to delete {}".format(s_name), icon='warning')
if answer == 'yes':
#deleted function here
else:
#not deleted function here
Run Code Online (Sandbox Code Playgroud)
如何突出显示"否"按钮而不是"是"按钮?
这里已经提出并回答了很多类似的问题,但浏览后没有一个完全解决我的问题。我正在寻找一种可靠的算法来找到每条由两个点指定的两条无限线的交点。就我而言,有两个并发症:
我目前的方法说明了基于斜率和截距的方法的缺点。这个问题可以通过实施一个旋转整个系统的步骤来部分规避,这样就没有线条是垂直的,但这看起来不太优雅。你知道更好的方法吗?
import numpy as np
# The intersection point of the example below should be (0,0)
# Vertices for the first line
p1_start = np.asarray([-5, 0])
p1_end = np.asarray([-3, 0])
# Vertices for the second line
p2_start = np.asarray([0, 4])
p2_end = np.asarray([0, 2])
# Calculate slope and intercept for the first line
m_1 = (p1_end[1]-p1_start[1])/(p1_end[0]-p1_start[0])
t_1 = p1_start[1] - m_1*p1_start[0]
# The slope and intercept are zero
print('First line')
print('slope = '+str(m_1)) …Run Code Online (Sandbox Code Playgroud) 我在python中使用assert.每次断言失败时,我都会得到失败的消息,我会把它放在那里打印.但我想知道在断言条件通过时是否有任何方法可以打印自定义成功消息?我正在使用py.test框架.
例:
assert self.clnt.stop_io()==1, "IO stop failed"
Run Code Online (Sandbox Code Playgroud)
对于上面的断言,如果断言失败,我得到消息"IO stop failed"但是如果assert通过,我希望"IO stop succeeded".就像是
assert self.clnt.stop_io()==1, "IO stop failed", "IO stop succeeded"
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我为什么我没有选择python3作为内核的选项吗?
# python3 -m ipykernel install --user
Installed kernelspec python3 in /root/.local/share/jupyter/kernels/python3
# python3 -m pip install ipykernel
Requirement already satisfied: ipykernel in /usr/lib/python3.4/site-packages
Requirement already satisfied: tornado>=4.0 in /usr/lib64/python3.4/site-packages (from ipykernel)
Requirement already satisfied: ipython>=4.0.0 in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: jupyter-client in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: traitlets>=4.1.0 in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: backports_abc>=0.4 in /usr/lib/python3.4/site-packages (from tornado>=4.0->ipykernel)
Requirement already satisfied: pexpect; sys_platform != "win32" in /usr/lib/python3.4/site-packages (from ipython>=4.0.0->ipykernel)
Requirement …Run Code Online (Sandbox Code Playgroud) 我正在尝试对熊猫数据帧进行一些简单的操作.我已将pandas导入pd并将numpy导入为np,并导入csv以创建名为'dfe'的数据框.
我已成功使用以下代码根据一个条件填充新列:
dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用类似的技术根据两个条件填充同一列时,我得到一个错误(&''bool'和'str'的不支持的操作数类型)
这是我对多条件版本的尝试:
dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" & dfe['Time'] <= "10:00:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)
我已经看过很多针对类似问题的解决方案,但是鉴于我刚刚开始并且希望有人可以给我一些关于为什么这不起作用的线索,它们对我来说有点太复杂了.
谢谢
python ×9
python-3.x ×2
tkinter ×2
assertion ×1
bounding-box ×1
class ×1
dialog ×1
geometry ×1
jupyter ×1
loops ×1
messagebox ×1
oop ×1
pandas ×1
profiling ×1
pygame ×1
pytest ×1
text ×1
unit-testing ×1