我正在使用Pascal中的简单集,并且只想输出集合的内容.每次运行代码时,我都会收到以下错误消息:'project1.lpr(17,13)错误:无法读取或写入此类型的变量'.
这是我的代码:
program Project1;
{$mode objfpc}{$H+}
uses
sysutils;
type TFriends = (Anne,Bob,Claire,Derek,Edgar,Francy);
type TFriendGroup = Set of TFriends;
Var set1,set2,set3,set4:TFriendGroup; x:integer;
begin
set1:=[Anne,Bob,Claire];
set2:=[Claire,Derek];
set3:=[Derek,Edgar,Francy];
writeln(set1);
readln;
end.
Run Code Online (Sandbox Code Playgroud)
输出集是否有特殊的方法/功能?
谢谢
我只是迭代一个外部文件(包含一个短语),并想看看是否存在一条线(其中有"爸爸"一词)如果我找到它,我想用"妈妈"替换它.这是我建立的程序......但我不确定它为什么不起作用?!
message_file = open('test.txt','w')
message_file.write('Where\n')
message_file.write('is\n')
message_file.write('Dad\n')
message_file.close()
message_temp_file = open('testTEMP.txt','w')
message_file = open('test.txt','r')
for line in message_file:
if line == 'Dad': # look for the word
message_temp_file.write('Mum') # replace it with mum in temp file
else:
message_temp_file.write(line) # else, just write the word
message_file.close()
message_temp_file.close()
import os
os.remove('test.txt')
os.rename('testTEMP.txt','test.txt')
Run Code Online (Sandbox Code Playgroud)
这应该是这么简单......这让我生气!谢谢.
我已经搜索了论坛,无法理解我是否可以使用以下构造将新条目插入到我的Python词典中...而不将其转换为列表.
for x in range(3):
pupils_dictionary = {}
new_key =input('Enter new key: ')
new_age = input('Enter new age: ')
pupils_dictionary[new_key] = new_age
print(pupils_dictionary)
Run Code Online (Sandbox Code Playgroud)
输出如下:
Enter new key: Tim
Enter new age: 45
Enter new key: Sue
Enter new age: 16
Enter new key: Mary
Enter new age: 15
{'Mary': '15'}
Run Code Online (Sandbox Code Playgroud)
为什么只有玛丽:15进入,其他人都没有?
谢谢/
试图创建一个简单的Hangman游戏,其中读取外部文本(称为words.txt),并将其中的字符串导入名为WordsArray的字符串数组中.
程序编译很好,但是,在显示填充数组的内容之前,它要求我两次输入文件名(参见下面的foreach循环)
在显示之前,有人可以确定为什么要问我两次文件名吗?
(另外,更一般地说,我的重构是否适用于这个简单的应用程序?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static string [] LoadWords()
{
bool repeat = true;
while (repeat)
{
Console.WriteLine("Please enter the name of a file:");
string filename = Console.ReadLine();
try
{
string[] WordsArray = File.ReadAllLines(filename);
if (WordsArray.Length == 0)
return null;
else
return WordsArray;
}
catch (FileNotFoundException msg)
{
Console.WriteLine("\n Check the file exists!");
}
}
return null;
}
static void DisplayWordsArray(string [] WordsArray) …Run Code Online (Sandbox Code Playgroud)