C
C#2y ago
Czkafek

Pi practice by google

Hey, I am trying to do this practice of writeing number Pi which google adds every year for 2-3 days when is Pi Day. I have no idea how to read in real time every thing that user write (and I want to do this this way that user dont have to click enter).
66 Replies
MODiX
MODiX2y ago
Method: System.Console.ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window. Method: System.Console.ReadKey(Boolean) Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
that second overload might be useful here or may not, depends if you want to print it or not. regardless, you can just do Console.ReadKey in a loop and you will read the users input one character at a time
Czkafek
CzkafekOP2y ago
how can I check by if statement which key did user clicked?
Czkafek
CzkafekOP2y ago
Czkafek
CzkafekOP2y ago
I mean if this good what should I put after "!="
Pobiega
Pobiega2y ago
public static void Main()
{
while (true)
{
var key = Console.ReadKey().KeyChar;

if (!char.IsNumber(key))
break;

// check your numbers here, I guess

}
}
public static void Main()
{
while (true)
{
var key = Console.ReadKey().KeyChar;

if (!char.IsNumber(key))
break;

// check your numbers here, I guess

}
}
Also, I just really have to ask.... Why this title for the post? Why not "reading console input without enter?" or something similar The fact thats its pi, or for a google thing is... not relevant?
Czkafek
CzkafekOP2y ago
bc I will for 85% ask for smth more and I thought that maybe someone will want to do same thing can u explain what this if statement checks?
Pobiega
Pobiega2y ago
isn't that obvious from the name of the function? the whole idea here is that the program keeps reading input one key press at a time, forever. you cant have a forever-loop without a break thou, so I made it so if you press a non-number char it quits since a isnt a valid digit of pi, for any digit position :p
Czkafek
CzkafekOP2y ago
yeah but I want to build program that checks every number like it says write now 15 number after decimal and user writes 3.14159263 and 8th number is wrong so It stops and write NO! its wrong (it should be 3.14159265)
Pobiega
Pobiega2y ago
okay... so do that then you can change the break condition. something like press Q to stop the program at any point seems nice
Czkafek
CzkafekOP2y ago
em idk how it helps in problem that Ive just wrote I want to check exacly what number user clicked
Pobiega
Pobiega2y ago
and thats exactly what that code does?
Czkafek
CzkafekOP2y ago
bassicly yes
Pobiega
Pobiega2y ago
no, you don't understand var key = Console.ReadKey().KeyChar; this will read one keypress
Czkafek
CzkafekOP2y ago
yes
Pobiega
Pobiega2y ago
but we run it in a loop, so it will trigger for every single keypress
Czkafek
CzkafekOP2y ago
yes
Pobiega
Pobiega2y ago
you can then look at the value of key and see what was pressed
Czkafek
CzkafekOP2y ago
so I can write just
if ( key != pi[i] )
if ( key != pi[i] )
?
Pobiega
Pobiega2y ago
yep
Czkafek
CzkafekOP2y ago
ow I thought it will be more complicated
Pobiega
Pobiega2y ago
Czkafek
CzkafekOP2y ago
why I didnt thought abt it u already did it?
Pobiega
Pobiega2y ago
yes it was trivial
Czkafek
CzkafekOP2y ago
ik but stil 💀 no
Pobiega
Pobiega2y ago
I had a much longer pi at first :p
Czkafek
CzkafekOP2y ago
i dont read it
Pobiega
Pobiega2y ago
okay.
Czkafek
CzkafekOP2y ago
thanks for deleting and for help
Pobiega
Pobiega2y ago
welcome is you here is a thousand digits of pi, in case ya need it 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
Czkafek
CzkafekOP2y ago
thanks I have next question XD is here a command that - u give a variable for example int i = 3 and it write every from string until the character that is on 3rd position in table? there was such a command in python but i dont remeber its name
Pobiega
Pobiega2y ago
sure, substring, or string slicing pi[..3] would get all the digits until the third position
Czkafek
CzkafekOP2y ago
Czkafek
CzkafekOP2y ago
good?
Pobiega
Pobiega2y ago
hm, given only a single digit I think substring starts at that position
MODiX
MODiX2y ago
Method: System.String.Substring(Int32) Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string. Method: System.String.Substring(Int32, Int32) Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
yep, thats the startindex
Czkafek
CzkafekOP2y ago
em okay so which command should I use?
Pobiega
Pobiega2y ago
^
Czkafek
CzkafekOP2y ago
just write it in consolewriteline?
Pobiega
Pobiega2y ago
yes?
Czkafek
CzkafekOP2y ago
okay and If I want to use i variable I just replace 3 with i yes?
Pobiega
Pobiega2y ago
yes when in doubt, $tias
Czkafek
CzkafekOP2y ago
should I delete those dodts? dots**
Pobiega
Pobiega2y ago
no... because then you'd be doing pi[3] and you know what that does, no?
Czkafek
CzkafekOP2y ago
Czkafek
CzkafekOP2y ago
let me translate u this
Pobiega
Pobiega2y ago
you dont need to
Czkafek
CzkafekOP2y ago
okay
Pobiega
Pobiega2y ago
why are you using .NET Framework
Czkafek
CzkafekOP2y ago
what should I use?
Pobiega
Pobiega2y ago
.NET
Czkafek
CzkafekOP2y ago
ow
Pobiega
Pobiega2y ago
$newproject
MODiX
MODiX2y ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Pobiega
Pobiega2y ago
Czkafek
CzkafekOP2y ago
tbh i clicked framework bc guy in the tutorial picked it
Pobiega
Pobiega2y ago
and this is why we dont follow old tutorials
Czkafek
CzkafekOP2y ago
4 years are old right?
Pobiega
Pobiega2y ago
in programming, yes but even then, making a .NET framework tutorial in 2019 kinda... sucks .NET core came out in 2016-2017-ish?
Czkafek
CzkafekOP2y ago
idk How can I do the pi[..3] command but it wont write everything in one second but it will write every number in like 1s or 0.7s? can I use a 1 year tutorial?
Pobiega
Pobiega2y ago
probably. I mean, just check what .NET version they are using if its 5, 6 or 7, you're fine you cant with a single command, you'll need to make your own method with a loop and Task.Delay wont be super simple.
Czkafek
CzkafekOP2y ago
good If everything is simple then it means u are not growing I think u know what I want to say So I will now end the program in this version the expand it with delays
Czkafek
CzkafekOP2y ago
okay so smth is not right in this code and tbh idk what I did wrong so can I ask u for check it?
Czkafek
CzkafekOP2y ago
brb please? I THINK I DID IT wait a sec yes I did it, mr Pobiega thank u for your time and help, I can go to sleep now in peace Hey now I am making this code better and I thought that maybe I can use tuples like in python? in python tuples are 10 times faster than normal list/table idk how to name it in english it was easier than I thought
Want results from more Discord servers?
Add your server