我正在使用Python制作一个Hangman游戏.在游戏中,一个python文件具有从数组中选择随机字符串并将其存储在变量中的函数.然后将该变量传递给另一个文件中的函数.该函数将用户猜测存储为变量中的字符串,然后检查该猜测是否在该单词中.但是,每当我输入一个字母并按回车键时,我会在此问题的标题中收到错误.你知道,我正在使用Python 2.7.这是接受一个单词的函数的代码:
import random
easyWords = ["car", "dog", "apple", "door", "drum"]
mediumWords = ["airplane", "monkey", "bananana", "window", "guitar"]
hardWords = ["motorcycle", "chuckwalla", "strawberry", "insulation", "didgeridoo"]
wordCount = []
#is called if the player chooses an easy game.
#The words in the array it chooses are the shortest.
#the following three functions are the ones that
#choose the word randomly from their respective arrays.
def pickEasy():
word = random.choice(easyWords)
word = str(word)
for i in range(1, len(word) + 1):
wordCount.append("_")
#is …Run Code Online (Sandbox Code Playgroud) 首先,我想摆脱我知道这个问题的方式:Python - tkinter 'AttributeError: 'NoneType' object has no attribute 'xview''
但是,在阅读本文后,我仍然对问题所在感到困惑。我有一个使用 Tkinter 的程序。其中有两个文本框,用户可以在其中输入文本。我希望这些框可以滚动。但是,我在执行此操作时遇到问题。这是我的代码:
from Tkinter import *
def main():
window = Tk()
window.title("TexComp")
window.geometry("500x500")
window.resizable(height=FALSE,width=FALSE)
windowBackground = '#E3DCA8'
window.configure(bg=windowBackground)
instruction = Label(text="Type or paste your text into one box,\nthen paste the text you want to compare it too\ninto the other one.", bg=windowBackground).place(x=115, y=10)
text1 = Text(width=25).pack(side=LEFT)
text2 = Text(width=25).pack(side=RIGHT)
scroll1y=Scrollbar(window, command=text1.yview).pack(side=LEFT, fill=Y, pady=65)
scroll2y=Scrollbar(window, command=text2.yview).pack(side=RIGHT, fill=Y, pady=65)
mainloop()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我在 scroll1y 和 scroll2y 滚动条上收到一条错误消息,指出“'NoneType' 对象没有属性 …
我在这个网站上搜索过类似的问题,而我发现的问题并不适用于我.我为询问答案是否在某个地方并且我无法找到而道歉.如果我这样做,请告诉我是否做错了.
我在C#做刽子手.我所做的就是让程序从一个数组中选取一个随机字符串,制作一个猜测字母的数组(它最初用"_"填充,只要这个单词是).然后它应该获得用户输入的字母,看看该字母是否在单词中,如果是,则将该字母添加到猜测的字母数组中.我被困在这个部分:
if (gameWord.Contains(guessedLetter))
{
//for every character in gameWord
for (int x = 0; x < gameWord.Length; x++)
{
//if the character at the 'x' position in gameWord is equal to the guessed letter
if (gameWord[x] == guessedLetter)
{
//guessString at that x position is equal to the guessed letter
guessString[x] = guessedLetter;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在" if (gameWord[x] == guessedLetter)"我收到标题中显示的错误.
gameWord是从字符串数组中选择的字符串,而guessedLetter是用户输入的字符串Console.ReadLine();.
我试图制作一个程序,当单击一个按钮然后在屏幕上显示输出时,它将生成一个随机数。但是,我无法使用JLabel将保存随机数的变量传递给类,以便可以在该类中使用它。我编写了一个程序,以使其易于演示:
import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main{
public static void main(String[] args){
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);
Panel panel = new Panel();
mainFrame.getContentPane().add(panel);
JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);
JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator());
}
static class numGenerator implements ActionListener{
public void ActionPerfomed (ActionEvent e){
int num; …Run Code Online (Sandbox Code Playgroud) 我正在用python编写一个程序。在其中,我想生成一个列表。此列表将从 1 开始并以 1 递增[1, 2, 3, 4, 5, etc.]。
但是,我希望列表的长度是随机的,介于 3 个数字和 8 个数字之间。例如,在一次运行中,列表可能会生成[1, 2, 3, 4],在另1, 2, 3, 4, 5, 6]一次运行中可能会生成,另一次运行可能会生成[1, 2, 3],等等。
我知道如何使列表生成随机数,但不知道如何以随机长度递增数字。感谢您的时间。顺便说一下,我正在使用 Python 2.7。